Forum: Ruby on Rails fields_for items not updating child records??

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D0b55f385fc038fc7fc588a9c59c3618?d=identicon&s=25 internetchris (Guest)
on 2009-04-07 00:37
(Received via mailing list)
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 -->
<h4>Property Information:</h4>
<div id="top_background1"><strong>Property No:</strong><br>
<%=f.text_field :s_account_no, :size => '10'%> </div>
<br>

<!--Customer Information-->

 <div id="Customer_info">


<h4>Customer Information</h4>
  <%fields_for @account.customer do |customer_fields|%>
<br>
<div style='padding-left:30px;margin-top:-12px;'>Name:        <
%=customer_fields.text_field :name, :size => '48'%></div><br>
<div style='padding-left:30px;margin-top:-12px;'>Address:        <
%=customer_fields.text_field :address, :size => '48'%></div><br>
<div style='padding-left:0px;margin-top:-15px;'>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'%></
div>
<div style="padding-left:425px"> </div>



<%end%>

  </div>
<br>
<br>
 <!--Owner Information-->

 <div id="Owner_info">


<h4>Owner Information</h4>
  <%fields_for @account.owner do |i|%>
<br>
<div style='padding-left:30px;margin-top:-12px;'>Name:        <
%=i.text_field :name, :size => '48'%></div><br>
<div style='padding-left:30px;margin-top:-12px;'>Address:        <
%=i.text_field :address, :size => '48'%></div><br>
<div style='padding-left:0px;margin-top:-15px;'>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'%></div>
<div style="padding-left:425px"> </div>

<%end%>

  </div>

<%= submit_tag "Update Account" %>
<%end%>



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

Chris
1e44d920287c3e26c3d7080769b5ccb5?d=identicon&s=25 Jaryl Sim (Guest)
on 2009-04-07 04:44
(Received via mailing list)
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 <ch...@silhouettesolutions.net>
D0b55f385fc038fc7fc588a9c59c3618?d=identicon&s=25 internetchris (Guest)
on 2009-04-07 19:41
(Received via mailing list)
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
This topic is locked and can not be replied to.