Rails 4: Insert Attribute Into Params

前端 未结 4 541
忘了有多久
忘了有多久 2020-12-12 18:36

In Rails 3, it was possible to insert an attribute into params like so:

params[:post][:user_id] = current_user.id

I\'m attempting to do som

4条回答
  •  死守一世寂寞
    2020-12-12 19:16

    Found the answer here. Rather than inserting the attribute from within the controller action, you can insert it into the params definition with a merge. To expand upon my previous example:

    private
    
      def post_params
        params.require(:post).permit(:some_attribute).merge(user_id: current_user.id)
      end
    

提交回复
热议问题