【问题标题】:Socialization Gem for Rails - How Can I Display the Number of Likes a Post has ReceivedRails 的社交宝石 - 我如何显示帖子收到的点赞数
【发布时间】: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


    【解决方案1】:

    您将错误的类传递给plate.likers。您应该通过喜欢该盘子的课程,例如:plate.likers(User).count

    【讨论】:

    • 非常简单有效:) 感谢您抽出宝贵时间提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多