Weird form behavior

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.

On Fri, Feb 11, 2011 at 10:58 AM, Tim U. [email protected] wrote:

%br/
%br/
If I do this in the controller it works fine.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Fri, Feb 11, 2011 at 4:07 PM, Jim Ruther N. [email protected]
wrote:

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.

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?

On Fri, Feb 11, 2011 at 4:38 PM, Jim Ruther N. [email protected]
wrote:

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”}]

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.

I’m not sure but if you look at

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.htmlyour
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”}]

On Fri, Feb 11, 2011 at 11:28 AM, Tim U. [email protected] wrote:

class User

to the user attr list
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Fri, Feb 11, 2011 at 4:40 PM, Tim U. [email protected] wrote:

On Fri, Feb 11, 2011 at 4:38 PM, Jim Ruther N. [email protected] wrote:

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”}]

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.
:smiley: at least you figured it out.

On Fri, Feb 11, 2011 at 6:05 PM, Tim U. [email protected] wrote:

“_destroy”=>“false”}]
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs