12

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"} 

5 Answers 5

25

Ok, the right answer is

change

 = f.fields_for :store do |store_fields| 

to

 = f.fields_for :store, @vendor.store do |store_fields| 
Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't this be: = f.fields_for :store_attributes, @vendor.store do |store_fields|
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

3 Comments

Following this post: stackoverflow.com/questions/15919761/… I think it should be params.require(:product).permit(:attributes, :of, :product, store_attributes: [:id, :name])
Take a look @ UPD please
To cosign what @AJcodex suggested. Get rid of the last .permit!
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.)

2 Comments

Take a look @ UPD please
get rid of the last permit!
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 ]) 

2 Comments

Take a look @ UPD please
@Zhirayr, You would not need the extra .permit!.
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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.