【发布时间】:2014-12-09 07:12:05
【问题描述】:
我在我的第一个 Ruby on Rails 项目中使用了Socialization gem。
该应用程序包含用户发布的照片。照片被放置在某种活动流中,很像 Instagram。
我已经成功实施了社会化。
用户可以点赞其他用户的帖子,其实就是一张照片,叫板。
如何识别和显示 Plate 收到的点赞数?
在我看来,以及我尝试的代码(最后一行)没有显示准确的数字,只返回 0:
<% if current_user.likes?(plate) %>
<%= link_to "Unlike", plate_unlike_path(plate), :method => :post, :class => 'btn btn-primary btn-xs' %>
<% else %>
<%= link_to "Like", plate_like_path(plate), :method => :post, :class => 'btn btn-primary btn-xs' %>
<% end %>
<br />
<p>Number of likes <%= plate.likers(Plate).count %></p>
来自社交控制器:
def like
target_plate=Plate.find(params[:plate_id])
current_user.like!(target_plate)
redirect_to plate_path(target_plate)
end
def unlike
target_plate=Plate.find(params[:plate_id])
current_user.unlike!(target_plate)
redirect_to plate_path(target_plate)
end
谢谢。
【问题讨论】:
标签: ruby-on-rails ruby