Rails 4: Insert Attribute Into Params
Asked Answered
B

4

70

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 something similar in Rails 4, but having no luck:

post_params[:user_id] = current_user.id

. . . .


private

  def post_params
    params.require(:post).permit(:user_id)
  end

Rails is ignoring this insertion. It doesn't throw any errors, it just quietly fails.

Buenabuenaventura answered 13/5, 2013 at 20:27 Comment(0)
B
151

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
Buenabuenaventura answered 13/5, 2013 at 22:12 Comment(5)
@marflar you used deep_merge within the params constructor? Or elsewhere in your controller?Syreetasyria
This is how I do it also but I keep thinking there must be a way to DRY this up a bit. All of my 50+ controllers have a similar .merge statement down in the strict params area. In my case I merge the current_user into updated_by. I merge current_user into created_by only in the create method.Limicoline
@Dan, I guess you use associations, so it should be easier to use something like: current_user.items.create(item_params). I use merge only when I have another association to create, i.e. @comment = @commentable.comments.new(comment_params) and my comment_params method looks like this: params.require(:comment).permit(:body, :parent_id, :removed).merge(user_id: current_user.id)Cumbrance
Guys, pls take a look at my question and help me out if you can. It closely relates to this one: #33358001Benildas
If like me you were looking for how to do it with custom value in params you can do ` params.require(:post).permit(:some_attribute).merge(user_id: params[:post][:id])`Heymann
C
40

In addition to @timothycommoner's answer, you can alternatively perform the merge on a per-action basis:

  def create
    @post = Post.new(post_params.merge(user_id: current_user.id))
    # save the object etc
  end

private
  def post_params
    params.require(:post).permit(:some_attribute)
  end
Calorifacient answered 13/6, 2014 at 16:57 Comment(3)
Hey, how would you do that for a nested resource ?Innocuous
i'm not sure why but @timothycommoner's answer doesn't work for me. only this one does... i even tried merge! and that still failed. oh well this reads easier anyway because there's no digging in private methods and it's easier to change in different use casesTentation
@Patient55 I guess you need deep_merge as they discussed in the selected answer's comments.Deponent
C
3

As an alternative for this case, you can required pass attribute via scope:

current_user.posts.create(post_params)

Cutty answered 12/7, 2016 at 10:29 Comment(0)
I
1

If anyone is trying to figure out how to add/edit a nested attribute in a Rails 5 attributes hash, I found this to be the most straight-forward (alternate) approach. Don't bother with merge or deep_merge...it's a pain due to the strong parameters. In this example, I needed to copy the group_id and vendor_id to the associated invoice (nested parameters) prior to saving.

def create
  my_params = order_params
  @order = Order.new
  @order.attributes = my_params
  @order.invoice.group_id = my_params[:group_id]
  @order.invoice.vendor_id = my_params[:vendor_id]
  @order.save
end

private

# Permit like normal
def order_params
  params.require(:order).permit([:vendor_id, :group_id, :amount, :shipping,:invoice_attributes => [:invoice_number, :invoice_date, :due_date, :vendor_id, :group_id]])
end
Incorporable answered 27/5, 2019 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.