I have a form with nested params. In the following example, how do I get the “amount_whole” value in the controller?
Parameters: {"utf8"=>"✓", "authenticity_token"=>"KCmBI6RLh0LdUsM2r5H1vhNykS1IXecFe5Lct+TuIGc=", "dec_declaration"=>{"declaration_nr"=>"SAL_2012_0001", "dec_declarationlines_attributes"=>{"0"=>{"amount_whole"=>"75"}}
Is it like this?
amount = params[:dec_declarations][:dec_declarationlines_attributes][:amount_whole]
You forgot the
"0"index in the hash. So you should be able to access it like this:The params hash works with both symbols and strings as keys.
Edit
However, judging buy the structure of the params it looks like you have a model called DecDeclaration which has_many DecDeclarationlines and accepts_nested_attributes for that association. So you should be able to use it like this in the controller:
Because if the params comes in that structure, it will automatically assign the nested values to the association.