Tim_Uckun
(Tim Uckun)
February 11, 2011, 2:58am
1
I am having a confounding issue and I hoping somebody can give me a
clue as to what is happening.
I have one field in my table which is not updating via the form. It
updates fine if I extract the information from the params and do it
manually in the controller. On this form the ebmedded form is also
not updating any of the child records but first I'd like to figure out
what is going on here.
Basically you have this User has_many :contact_methods.
The form looks like this.
- form_for (@user , :url => edit_profile_url) do |f|
= f.label :email
%br/
= f.text_field :email, :class => "text-input"
%br/
= f.label :name
%br/
= f.text_field :name, :class => "text-input"
%br/
= f.label :location
%br/
= f.text_field :location, :class => "text-input"
%br/
= f.label :skills_list , 'My skills are (comma separated list)'
%br/
= f.text_field :skill_list, :label => 'My skills are (comma
separated list)' , :class => "text-input"
%br/
= f.label :bio
%br/
= f.text_field :bio, :label => ' Brief personal summary' , :class
=> "text-input"
%br/
= render 'contact_methods' , {:f => f}
=f.submit
Every field in here is updatable except the location field. For some
odd reason it won't update the location field when you submit the
form.
If I do this in the controller it works fine.
@user.update_attributes (params[:user])
location = params[:user][:location]
@user.location = location if location
so why is only this one field not updating while everything else is?
Is "location" a special term?
i don’t know if location is a special term or not. But I have a question, do have an attr_accessible line in your user model?
If you do, check that you have the location attribute listed there.
Tim_Uckun
(Tim Uckun)
February 11, 2011, 3:28am
3
DOH!. Yes that was the problem...
Now for the second part....
class User
has_many :contact_methods
accepts_nested_attributes_for :contact_methods, :allow_destroy => true
end
The form has this (from above)
= render 'contact_methods' , {:f => f}
the partial looks like this.
= f.fields_for :contact_methods do |c|
%tr
%td
= c.object.icon.nil? ? nil : image_tag(c.icon) #
%td
= c.text_field :details ,:class => "text-input"
%td
- unless c.object.new_record?
= c.hidden_field :_destroy
= link_to 'Delete', '#', :class => 'delete_contact_method'
This displays the records fine, submits them as
"contact_methods_attributes"=>{"0"=>{"details"=>"test", "id"=>"1",
"_destroy"=>"false"}} but it won't update them.
There is no error message and @user.save ! does not throw an exception.
I created the contact method by hand but it won't delete it or update it.
The ContactMethod has no attr_accessable and I added :contact_methods
to the user attr list
So why doesn't the nested_atrributes work?
I’m not sure but if you look at
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
your params does not have the same format as the one stated in the link. It should be
“contact_methods_attributes”=>[{“details”=>“test”, “id”=>“1”,
“_destroy”=>“false”}]
Tim_Uckun
(Tim Uckun)
February 11, 2011, 3:40am
5
That's interesting. I thought fields_for would have taken care of that
automatically.
I'll try manually building the fields and see what happens.
Tim_Uckun
(Tim Uckun)
February 11, 2011, 10:05am
6
The problem turned out to be attr_accessible You have to put
contact_methods_attributes in there and not contact_methods.
oh right! we forgot about that one when we were discussing it minutes ago. at least you figured it out.