0

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
Leon
  • 161
  • 1
  • 11
  • What is the relation between `Item` and `LineTems`? – Pavan May 30 '14 at 19:58
  • item is just a field in the record – Leon May 30 '14 at 19:59
  • Then you can just give `params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider,:items => {})` – Pavan May 30 '14 at 20:00
  • It does not work. I updated with the code from the model my question. I still get Unpermitted parameters: items – Leon May 30 '14 at 20:01
  • Have you tried my updated comment? – Pavan May 30 '14 at 20:02
  • params.require(:line_items).permit! works just fine – Leon May 30 '14 at 20:03
  • This `params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider,:items => {})` didn't worked? – Pavan May 30 '14 at 20:04
  • Now i get Unpermitted parameters: price, name, quantity – Leon May 30 '14 at 20:04
  • params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider,:items => {}) and Then params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider,:items => {:price, :name, :quantity}) – Leon May 30 '14 at 20:06
  • Just give like this `params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider,{:items => {}})` – Pavan May 30 '14 at 20:08
  • I don't see `:key1, :key2, :key3, :key4, :payment_type, :payment_provider` in your parameters log.Then why you are giving those attributes? – Pavan May 30 '14 at 20:13
  • I receive those i just did not post them here. and they all work fine. This is the only params that is creating a problem. – Leon May 30 '14 at 20:16
  • Please post your full parameters log. – Pavan May 30 '14 at 20:16
  • Parameters: {"access_token"=>"fd8f25e9d29ca1793c05bbacd0e6aa5279f46387596ebc1c3e09d08e86a2f62365gf", "contentType"=>"application/json", "line_items"=>{"merch_name"=>"Leon Shop", "merch_address"=>"123 street new york", "items"=>{"0"=>{"price"=>"11.50", "name"=>"Item1", "quantity"=>"10"}, "1"=>{"price"=>"3.38", "name"=>"Item2", "quantity"=>"2"}, "2"=>{"price"=>"1.98", "name"=>"Item3", "quantity"=>"2"}}, "payment_type"=>"Credit Card"}} – Leon May 30 '14 at 20:21

1 Answers1

1

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.

Community
  • 1
  • 1
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • Now i get this: Unpermitted parameters: price, name, quantity Unpermitted parameters: price, name, quantity Unpermitted parameters: price, name, quantity as the hash has 3 elements – Leon May 30 '14 at 20:13
  • lineitems_controller.rb:171: syntax error, unexpected $end, expecting keyword_end when i tried your updated version. By the way thanks for taking time to help – Leon May 30 '14 at 20:18
  • @Leon Now please check. – Pavan May 30 '14 at 20:29
  • I have tried that too and it still throws the same error. – Leon May 30 '14 at 21:19
  • So i tried your solution even before, and what i see is that the record updates but i still get the Unpermitted parameters: items, in my log. I don't know why i still get that though.fow now i will just leave it like this as it works. Not sure why it is still throwing the error. – Leon May 30 '14 at 21:30