【问题标题】:How do I assign a comment to current_user?如何为 current_user 分配评论?
【发布时间】:2011-04-06 02:46:01
【问题描述】:

我的create 操作中有这一行

@comment = @video.comments.new(params[:comment])

评论也属于创建它的用户。如何将其分配给 current_user?它在哪里适合这段代码?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 variable-assignment instance-variables


    【解决方案1】:
     @video.comments.new(params[:comment].merge({:user_id => current_user.id}))
    

    我正在使用Hash#merge

    【讨论】:

    • 很好,我也可以在我的destroy 操作中将此附加到@comment = @video.comments.find(params[:id]) 吗?
    • 你想做的事:@video.comments.find(params[:id], :conditions => {:user_id => current_user.id})
    【解决方案2】:

    你也可以这样做:

    @comment = @video.comments.new(params[:comment])
    @comment.user = current_user
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 1970-01-01
      相关资源
      最近更新 更多