Rails将附加参数传递给模型

Mat*_*zzi 5 ruby activerecord model ruby-on-rails

在控制器中我有一个创建动作

  def create

    params[:note]
    @note = current_user.notes.new(params[:note])

      if @note.save
        respond_with @note, status: :created
      else
        respond_with @note.errors, status: :unprocessable_entity
      end
  end
Run Code Online (Sandbox Code Playgroud)

我想传递给模型另一个名为current_user的参数,如何做到这一点以及如何在模型方法中检索传递的参数?

Mik*_*Mik 3

@note = Note.new(params[:note].merge(:user_id => current_user.id))
Run Code Online (Sandbox Code Playgroud)

但这也许不是最好的方法,看看这个:Adding variable to params in Rails

如果您想访问模型中的 current_user,请参阅:Rails 3 devise, current_user is not access in a Model ?