Fields_for items not updating child records?


#1

Hey everyone,

I need some help. I have an accounts controller, and am working with
the show view. Each account has an “owner” and a “customer”. I can
create the accounts/owners/customers just fine, but when it comes to
updating I can’t. I have a single account view, that uses a form, and
the “fields_for” command to update the customer and owners when I
submit the form. The account fields update just fine, but the child
records (owner, and customer) information does not. Here’s my update
routine in the controller. I’m pretty new so I’m sure it’s something
simple I’m missing.

*******account_controller

def update

@account = Account.find(params[:id])
if @account.update_attributes(params[:account])
flash[:notice] = ‘Account Record Saved’
redirect_to(:action=>‘show’)
else
flash[:warning] = ‘Account Record Did Not Save!’
redirect_to(:action=>‘show’)
end

end

*******account_“show”_view

<% form_for(:account, @account, :url => {:action => ‘update’}, :html
=> { :multipart => true, :method => :put }) do |f| %>

Property Information:

Property No:
<%=f.text_field :s_account_no, :size => '10'%>

Customer Information

<%fields_for @account.customer do |customer_fields|%>
Name: < %=customer_fields.text_field :name, :size => '48'%>

Address: < %=customer_fields.text_field :address, :size => '48'%>

City/State/Zip: < %=customer_fields.text_field :city, :size => '28'%>,< %=customer_fields.text_field :state, :size => '2', :maxlength => '2'%> <%=customer_fields.text_field :zip, :size => '5', :maxlength => '5'%>

<%end%>



Owner Information

<%fields_for @account.owner do |i|%>
Name: < %=i.text_field :name, :size => '48'%>

Address: < %=i.text_field :address, :size => '48'%>

City/State/Zip: < %=i.text_field :city, :size => '28'%>,<%=i.text_field :state, :size => '2', :maxlength => '2'%> <%=i.text_field :zip, :size => '5', :maxlength => '5'%>

<%end%>

<%= submit_tag “Update Account” %>
<%end%>

Let me know if you need more info. I’m hoping I missed something easy.

Chris


#2

You might want to try inspecting the params in the controller, add a
“raise params.inspect” into your update action.

I suspect that it is because you have not provided fields for with the
parent form, like so:

<% f.fields_for @account.owner do |i|%>

If you don’t put the “f.” in front, rails will simply generate params
that you can access via “params[:owner]”, whereas you want something
like “params[:account][:owner]”.

This is one problem, but there might be more.

On Apr 7, 6:37 am, internetchris removed_email_address@domain.invalid


#3

Ok so I made those changes, and I am still having issues. Rather than
posting the problem on two separate forums, I will simply provide a
link to the latest problem on the railsforum. Any help is very
appreciated. Multiple models on a single form seem to be hard to
accomplish.

http://railsforum.com/viewtopic.php?id=29044

Any help is greatly appreciated.

Chris