简体   繁体   English

干净地增加! 用户单击link_to image_tag时的模型属性

[英]Cleanly increment! attribute on model when user clicks on a link_to image_tag

I have an Advert model that basically has an image, title and a url. 我有一个广告模型,基本上有一个图像,标题和网址。 I use CarrierWave for the image. 我使用CarrierWave作为图像。 Everything works well, but I am wanting to count impressions and clicks . 一切运作良好,但我想要计算展示次数点击次数


So far, I added two integer columns: impressions, clicks 到目前为止,我添加了两个整数列:展示次数,点击次数

I have a pages controller and have set an @advert variable that changes depending on the page. 我有一个页面控制器,并设置了一个根据页面更改的@advert变量。 After reading this question I am able to increment impressions: 阅读此问题后,我可以增加展示次数:

 def index  
   @advert = Advert.sample  
   @advert.increment! :impressions  
 end   

I am having trouble incrementing the clicks | 我无法增加点击次数 Below is the code I need to modify so that when a user clicks on the image_tag they are taken to the URL as normal, but that the @advert increments the clicks column. 下面是我需要修改的代码,以便当用户点击image_tag时,他们会正常地访问URL,但@advert会增加点击列。 Here is my current code (I use HAML and this is code is in a file: _advert.html.haml): 这是我当前的代码(我使用HAML,这是代码在文件中:_advert.html.haml):

  = link_to image_tag("#{advert.foto_url.to_s}",:alt => "an image ad linking to #{advert.url}", :title => advert.title), advert.url, :target => "blank"

What is the easiest, cleanest way to successfully increment! 什么是最简单,最干净的成功增量方式! the clicks attribute on the Advert when someone clicks on the link above? 当有人点击上面的链接时,广告上的点击属性?

Update: 更新:

I am using the suggestion from Aaron below, but it leads to two new concerns: 我正在使用下面Aaron的建议,但它引出了两个新的担忧:

  • The user will see a different link when they hold their mouse over the image than the final link they will end up at. 当用户将鼠标悬停在图像上时,用户将看到不同的链接,而不是最终链接。 I would prefer to have them see the final URL they will end up if they click rather than Redirecting simply to count their click. 我希望让他们看到最终的网址,如果他们点击而不是重定向只是计算他们的点击,他们将会结束。

  • If someone pulls up the advert url (eg: /adverts/1) directly the counter would increment! 如果有人直接拉出广告网址(例如:/ adverts / 1),计数器会递增! without a click. 没有点击。

I actually didn't have a show view for Advert, but added it to get this done. 我实际上没有广告的展示视图,但添加它来完成这项工作。 So to be clear this works, but it feels like it violates best practices. 所以要清楚这是有效的,但感觉它违反了最佳实践。 Thank you though to Aaron. 谢谢Aaron。

I think the easiest way would be to make the advert image link to the advert show action, which could increment the counter and then redirect to the advert url. 我认为最简单的方法是将广告图片链接到广告节目操作,这可以增加计数器然后重定向到广告网址。

In your link, change advert.url to advert_path(advert) (and set up the necessary route) then in adverts_controller : 在您的链接中,将advert.url更改为advert_path(advert) (并设置必要的路由),然后在adverts_controller

def show
  @advert = Advert.find(params[:id])
  @advert.increment! :clicks
  redirect_to @advert.url
end

首先想到的是添加JavaScript,以便在单击该链接时,它会异步地向您的服务器发送请求,然后允许链接按设计完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM