Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何获取Liker的用户名?_Ruby On Rails_Ruby_Model View Controller - Fatal编程技术网

Ruby on rails 如何获取Liker的用户名?

Ruby on rails 如何获取Liker的用户名?,ruby-on-rails,ruby,model-view-controller,Ruby On Rails,Ruby,Model View Controller,这里的示例行不起作用。我试了20种 views/comments/_comments.html.erb <% @comments.each do |comment| %> <%= User.find(comment.user_id).name %> # Gives user name of commenter <%= simple_format comment.content %> <%= pluralize(comment.likes

这里的示例行不起作用。我试了20种

views/comments/_comments.html.erb

<% @comments.each do |comment| %>
  <%= User.find(comment.user_id).name %>    # Gives user name of commenter
  <%= simple_format comment.content %>
  <%= pluralize(comment.likes, 'like') %>
  <%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + 
    ' Like it', like_comment_path(:id => comment.id), method: :post %>
  <%= User.find(like.user_id).name %>       # How to get user name of liker?
<% end %>
def like
  @comment = Comment.find(params[:id])
  comment_like = current_user.comment_likes.build(comment: @comment)
  if comment_like.save
    @comment.increment!(:likes)
    @comment.create_activity :like
    @user = User.find(params[:id])
    flash[:success] = 'Thanks for liking!'
  else
    flash[:error] = 'Two many likes'
  end  
    redirect_to(:back)
end
class CommentLike < ActiveRecord::Base
   belongs_to :comment
   belongs_to :user
   validates :user, uniqueness: { scope: :comment }
end
class Comment < ActiveRecord::Base
    include PublicActivity::Common
    # tracked except: :update,  owner: ->(controller, model) { controller && controller.current_user }
    has_many :comment_likes 
    has_many :likers, through: :comment_likes, class_name: 'User'
    belongs_to :commentable, polymorphic: true
    belongs_to :user
end
类似于.rb的评论

<% @comments.each do |comment| %>
  <%= User.find(comment.user_id).name %>    # Gives user name of commenter
  <%= simple_format comment.content %>
  <%= pluralize(comment.likes, 'like') %>
  <%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + 
    ' Like it', like_comment_path(:id => comment.id), method: :post %>
  <%= User.find(like.user_id).name %>       # How to get user name of liker?
<% end %>
def like
  @comment = Comment.find(params[:id])
  comment_like = current_user.comment_likes.build(comment: @comment)
  if comment_like.save
    @comment.increment!(:likes)
    @comment.create_activity :like
    @user = User.find(params[:id])
    flash[:success] = 'Thanks for liking!'
  else
    flash[:error] = 'Two many likes'
  end  
    redirect_to(:back)
end
class CommentLike < ActiveRecord::Base
   belongs_to :comment
   belongs_to :user
   validates :user, uniqueness: { scope: :comment }
end
class Comment < ActiveRecord::Base
    include PublicActivity::Common
    # tracked except: :update,  owner: ->(controller, model) { controller && controller.current_user }
    has_many :comment_likes 
    has_many :likers, through: :comment_likes, class_name: 'User'
    belongs_to :commentable, polymorphic: true
    belongs_to :user
end
类CommentLike comment.rb

<% @comments.each do |comment| %>
  <%= User.find(comment.user_id).name %>    # Gives user name of commenter
  <%= simple_format comment.content %>
  <%= pluralize(comment.likes, 'like') %>
  <%= link_to content_tag(:span, '', class: 'glyphicon glyphicon-thumbs-up') + 
    ' Like it', like_comment_path(:id => comment.id), method: :post %>
  <%= User.find(like.user_id).name %>       # How to get user name of liker?
<% end %>
def like
  @comment = Comment.find(params[:id])
  comment_like = current_user.comment_likes.build(comment: @comment)
  if comment_like.save
    @comment.increment!(:likes)
    @comment.create_activity :like
    @user = User.find(params[:id])
    flash[:success] = 'Thanks for liking!'
  else
    flash[:error] = 'Two many likes'
  end  
    redirect_to(:back)
end
class CommentLike < ActiveRecord::Base
   belongs_to :comment
   belongs_to :user
   validates :user, uniqueness: { scope: :comment }
end
class Comment < ActiveRecord::Base
    include PublicActivity::Common
    # tracked except: :update,  owner: ->(controller, model) { controller && controller.current_user }
    has_many :comment_likes 
    has_many :likers, through: :comment_likes, class_name: 'User'
    belongs_to :commentable, polymorphic: true
    belongs_to :user
end
class注释(控制器,模型){controller&&controller.current_user}
有很多:你喜欢的评论
有很多:喜欢者,通过::评论喜欢者,类名:'User'
属于:可注释,多态:真
属于:用户
终止
1)如果控制器中声明了
like
变量,则该变量应为全局变量:
@like

2) 不要使用
User.find(comment.User\u id).name
User.find(like.User\u id).name

像注释.user.name,
@like.user.name

那样使用它,作为简短的答案

在控制器中,更改

  comment_like = current_user.comment_likes.build(comment: @comment)
  if comment_like.save

在你看来,改变

<%= User.find(like.user_id).name %>



一条评论有很多相似之处。所以你在寻找喜欢评论的人

class Comment
  has_many :comment_likes
  has_many :likers, through: :comment_likes, class_name: 'User', source: :liker
end

class CommentLikers
  belongs_to :liker, class_name: 'User', foreign_key: :user_id
  belongs_to :liked_comment, class_name: 'Comment', foreign_key: :comment_id
end

class User
  has_many :comment_likes
  has_many :liked_comments, through: :comment_likes, class_name: 'Comment', source: :liked_comment
end
那么在你看来,

<% @comments.each do |comment| %>
  <% comment.likers.each do |user| %>
    <%= user.name %>
  <% end %>
<% end %>

编辑1

您的问题在rails指南中得到了很好的解释:

但我只想给你一个简短的解释:

您需要通过评论类将评论与用户关联。为此,您需要告诉rails使用user_id在表中查找用户

这是通过::comment_喜欢的,类:'User'
所做的

编辑2

查看源代码以查看更多选项:

这些讨厌的东西是怎么回事?我一直很难回答这个问题。我是否缺少代码?我没有投否决票,但我在一两天前看到了你的问题,似乎你没有采纳给你答案的人的建议,因为你的视图代码中还有
User.find(comment.User\u id)
。感谢@MikeManfrin的解释。我说我最终会做到的::这给出了:nil:NilClass的未定义方法“user”,下面是为什么这是
的详细答案,谢谢Aguardientico!我尝试了一下,但是得到了nil:NilClass
@AnthonyGalli.com的
未定义的方法“user”,我发现了我的错误,在你的控制器中,你正在重定向到
back
,你能把
back
调用的动作放进去吗?aguardietico
重定向到(:back)
刷新页面,这取决于它是否是估值显示,习惯展示,目标展示,量化展示。我可能会离开show,允许用户直接在activities\u controller索引上喜欢/评论。我了解了你,我问了你关于操作的问题,因为在每个操作中,你应该添加代码来获得
like(comment\u like)
变量,但我想,更好的答案是使用Jorge de lo Santos的答案。很好,试试Jorge!非常感谢。我得到了这个,虽然
未定义的方法'each'对于2:Fixnum
对于line
Jorge,您有一个小错误,在model
Comment
中你有
likers
但是在视图中你尝试
Comment.likes
而不是
Comment.likers
所以@AnthonyGalli.com如果你尝试改变可能会奏效。当我在Comment.rb中放入
有很多:likers,通过::Comment\u likes
,user.rb&commentlike.rb我们得到:
未定义的方法“klass”用于nil:NilClass
,尽管@aguardidenco对第
@JorgedelosSantos行建议进行了更改,我一直在阅读您的链接。谢谢你!很遗憾,您的代码引发了错误:
在模型CommentLike中找不到源关联“liker”或:likers。试试“has_many:likers,:through=>:comment_likes,:source=>”。它是comment还是user?
如果我按照我的建议在comment\u like.rb中添加代码,
有很多:likers,:through=>:comment\u likes,:source=>“comment”
我们得到了nil:NilClass的
未定义的方法“klass”。也许是因为评论是多态的?我更新了问题:)这是一个很好的问题,但您需要完整的代码片段。给你发了一封电子邮件。补充了另一条线索。