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"}
Ok, the right answer is
change
= f.fields_for :store do |store_fields|
to
= f.fields_for :store, @vendor.store do |store_fields|
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 ]])
params.permit(:product => [ :name, :description, :permalink, :store_attributes => [:store_id, :supplier_id, :margin, :discount]])
http://edgeapi.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With