Leon
Leon

Reputation: 161

Rails 4 Unpermitted parameters

I have the following dynamic params depending on the line items i am trying to add to an order

{"line_item" => {"items"=>{"0"=>{"price"=>"5.75", "name"=>"Item name", "quantity"=>"5"}, "1"=>{"price"=>"3.35", "name"=>"Item name", "quantity"=>"1"}}}

In my controller:

def lineitems_params
  params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider).tap do |whitelisted|
    whitelisted[:items] = params[:line_item][:items]
  end
end

I still get the

Unpermitted parameters: items

in my logs, and it does not update the items. How can i solve this?

NOTE: the items hash can have many elements inside.

EDIT:

In my model:

serialize :items, Hash

Upvotes: 0

Views: 710

Answers (1)

Pavan
Pavan

Reputation: 33542

This should work

def lineitems_params

params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider, {:items => {:price, :name, :quantity}})

end

Update

may be you should just give like this

def lineitems_params
  params.require(:line_item).tap do |whitelisted|
    whitelisted[:items] = params[:line_item][:items]
  end
end

Source

Note: Don't give params.require(:line_items).permit! it permits all attributes.

Upvotes: 1

Related Questions