1

我有观点

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

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

像这样:

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

标题点击:45

4

2 回答 2

3

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

比在文章显示方法中(我认为您使用此方法显示文章):

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 %> 

或者,您可以通过模型回调来做到这一点。

于 2013-04-10T14:15:18.723 回答
0

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

<% link_to g.title do %>
  Click <%= countclick %>
<% end %>
于 2013-04-10T13:33:43.627 回答