Ruby on rails 将用户附加到评论

Ruby on rails 将用户附加到评论,ruby-on-rails,ruby-on-rails-3,comments,Ruby On Rails,Ruby On Rails 3,Comments,我有一个多态的有效评论系统,可以将评论添加到用户和帖子。但是,我无法确定如何将登录用户(评论者)附加到评论 注释\u controller.rb: class CommentsController < ApplicationController before_filter :authenticate_user!, :only => [:create, :destroy] def index @commentable = find_commentable @comments

我有一个多态的有效评论系统,可以将评论添加到用户帖子。但是,我无法确定如何将登录用户(评论者)附加到评论

注释\u controller.rb:

class CommentsController < ApplicationController
before_filter :authenticate_user!, :only => [:create, :destroy]

def index
  @commentable = find_commentable
  @comments = @commentable.comments
end

def new
    @commentable = find_commentable
end

def create
  @commentable = find_commentable
  @comment = @commentable.comments.build(params[:comment])
  if @comment.save
    flash[:notice] = "Successfully created comment."
    redirect_to get_master
  else
    render :action => 'new'
  end
end

def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to comments_url }
      format.json { head :no_content }
    end
end

protected

def find_commentable
  params.each do |name, value|
    if name =~ /(.+)_id$/
      return $1.classify.constantize.find(value)
    end
  end
  nil
end

def get_master
    @parent = @comment.commentable
    if @parent.respond_to?('commentable_type')
      @comment = @parent
      get_master
    else
      return @parent
    end
end
end
class CommentsController[:创建,:销毁]
def索引
@可评论的=查找可评论的
@comments=@commentable.comments
结束
def新
@可评论的=查找可评论的
结束
def创建
@可评论的=查找可评论的
@comment=@commentable.comments.build(参数[:comment])
if@comment.save
flash[:notice]=“已成功创建注释。”
重定向\u以获取\u主机
其他的
呈现:操作=>“新建”
结束
结束
def销毁
@comment=comment.find(参数[:id])
@毁灭
回应待办事项|格式|
format.html{redirect_to comments_url}
format.json{head:no_content}
结束
结束
受保护的
def find_可注释
参数。每个do |名称、值|
如果名称=~/(.+)\u id$/
返回$1.classify.constantize.find(值)
结束
结束
无
结束
def get_master
@parent=@comment.commentable
如果@parent.response\u to?(“可评论的类型”)
@comment=@parent
取得硕士学位
其他的
返回@parent
结束
结束
结束

这难道不像在
保存之前添加它那样简单吗

@comment.commenter = current_user
您还可以将其添加到视图中:

f.hidden_field :commenter_id, value: current_user.id
我想我更喜欢把它放在视野里