点击按轨道计数链接文章



我有视图

 <% @r.each do |g|  %>
   <%= link_to g.title %>
 <% end %>

当我点击链接时,我不会显示有多少人点击了这个链接。

:

 <%= link_to g.title %> Click:<%= countclick %>

标题点击:45

文章应该有countclicks:integer数据库列。

Than in article show method(我想你用这个方法来显示文章):

def show
@article = Article.find(params[:id])
count=@article.countclicks+1
@article.update_attributes(:countclicks=> count)
#other code
end

然后在索引视图中:

<% @articles.each do |article| %>
<%= link_to "#{article.name}", show_article_path(article)%>
<%=article.countclick %>
<% end %> 

如果你想这样使用link_to,你必须给它一个block:

<% link_to g.title do %>
  Click <%= countclick %>
<% end %>

最新更新