Trying to perform a nested object form. The page loads with no errors, but when I send it, no information gets saved to the organization model.
The SQL call says this ..
Parameters: {"commit" => "save", "action"=>"update","_method"=>"put", "organization"=>{"likes_snacks"=>"0"}, ..
Which is right. The 1 and 0 can be changed properly by flipping on and off the checkbox. But that information is just not saved to the database I guess. Any ideas?
HAML:
- form_for @user do |f|
= f.label :username
= f.text_field :username
.clear
- fields_for :organization do |org| unless @user.organizations.empty?
= org.label :likes_snacks, 'Like snacks?'
= org.check_box :likes_snacks
= f.submit 'save', {class => 'button'}
CONTROLLER:
def edit
@user = current_user
@organization = current_user.organizations.first
end
MODELS:
ORGANIZATION.RB:
has_many :users, :through => :organizations_users
USER.RB:
has_many :organizations, :through => :organizations_users
It seems like you can save the parent attributes but not the child attributes.
To make child attributes accessible through a nested forms you’ll need to add the “#{child_class_name}_attributes” to the attr_accessible method in your parent class.(Only if use
attr_accessiblein parent model)So your parent model should look like this:
Also, If you don’t use
attr_accessiblein your parent model this is not necessary.