9

I'm in need to setup attributes for has one association in new and edit actions, so I have this:

Product model

has_one :store
accepts_nested_attributes_for :store

form

= form_tag @product do |f|
  = f.fields_for :store do |store_fields|
    = render 'store_form', :f => store_fields

in controller

params.require(:store).permit(:store).permit!

fields displays, but when I'm submitting form, it doesn't make sense, store association is empty. How problem can be solved?

UPD

params.require(:product).permit(store_attributes: [:store_id, :supplier_id, :margin, :discount]).permit!

Logs:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "product"=>{"name"=>"qwefqwefasdf", "description"=>"", "permalink"=>"asdf", "store_attributes"=>{"margin"=>"123", "discount"=>"123"}}, "button"=>"", "id"=>"asdf"}
Mark Pegasov
  • 5,109
  • 9
  • 26
  • 30

5 Answers5

23

Ok, the right answer is

change

  = f.fields_for :store do |store_fields|

to

  = f.fields_for :store, @vendor.store do |store_fields|
Mark Pegasov
  • 5,109
  • 9
  • 26
  • 30
5

Make sure the params you expect are being sent. (check pluralization)

Can you copy and paste what the params look like from the server side?

13:44:29 INFO:   Parameters: {"utf8"=>"✓" .......

That will help to get the naming the params correctly

If params naming is correct, but not being accepted for, then try specifying them explicitly

params.permit(:product => [:something, :stores_attributes => [:name, :address ]])

Update:

params.permit(:product => [ :name, :description, :permalink, :store_attributes => [:store_id, :supplier_id, :margin, :discount]])

Nested Attributes Examples:

http://edgeapi.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit

Andrew Wei
  • 2,020
  • 1
  • 17
  • 28
  • Following this post: http://stackoverflow.com/questions/15919761/rails-4-nested-attributes-unpermitted-parameters I think it should be `params.require(:product).permit(:attributes, :of, :product, store_attributes: [:id, :name])` – MrYoshiji Dec 19 '13 at 19:00
  • Take a look @ UPD please – Mark Pegasov Dec 19 '13 at 19:09
  • To cosign what [@AJcodex suggested](http://stackoverflow.com/questions/20689478/nested-attributes-for-has-one-association/20689553#comment30985361_20689630). Get rid of the last .permit! – Andrew Wei Dec 19 '13 at 19:25
0

Assuming the controller in question is ProductsController, your strong parameter definition is incorrect.

Try:

params.require(:product).permit(:store_attributes)

Or, to be more strict, permit only the required attributes:

params.require(:product).permit(store_attributes: [ :store_field1, :store_field2 ])

Where :store_field1 and :store_field2 are the attributes from store model that are in your form and you want to permit.

Update:

The following should work based on your log output.

params.require(:product).permit(:name, :description, :permalink, store_attributes: [ :margin, :discount ])
vee
  • 38,255
  • 7
  • 74
  • 78
0

Look at the parameters in the console. You should see something like:

{ "product" => { "store_attributes" => {  } }

Which means you want to require :product (you want the product parameters) and permit the correct attributes for the store.

params.require(:product).permit(:store_attributes => [ :name, :location, :etc ])

When you require(:store) that means you expect a "store" key at the root of the parameters hash, which is not the case (and why your association is coming up empty.)

AJcodez
  • 31,780
  • 20
  • 84
  • 118
0

no this will not work. If you use nested_attributes with has_one association the these attributes should be written in plain not in the separate array.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 12 '23 at 00:21