开发者

Can't get plain old many-to-one nested form to save

I'm on Rails 3. I have a model Client that has an address_id. In my Client form, I have nested Address fields. Here's that the Address part of the form:

  <%= f.fields_for @client.address do |address_form| %>
    <div class="field">
      <%= address_form.label :line1 %><br />
      <%= address_form.text_field :line1 %>
    </div>
    <div class="field">
      <%= address_form.label :line2 %><br />
      <%= address_form.text_field :line2 %>
    </div>
    <div class="field">
      <%= address_form.label :city %><br />
      <%= address_form.text_field :city %>
    </div>
    <div class="field">
      <%= address_form.label :state_id %><br />
      <%= select("client[address]", "state_id", State.all.collect {|s| [ s.name, s.id ] }) %>
    </div>
    <div class="field">
      <%= address_form.label :zip %><br />
      <%= address_form.text_field :zip %>
    </div>
  <% end %>

When I try to save my form, I get this:

Address(#23652762896420) expected, got ActiveSupport::HashWithIndifferentAccess(#23652751466220)

I don't understand exactly what that means or why I'm getting it. Here's what params.inspect looks like:

{"commit"=>"Update Client",
 "authenticity_token"=>"CBw1fQcsUtXs9x6lKTL4zeoekX1cwuFUrZvZpCShHIc=",
 "_method"=>"put",
 "utf8"=>"\342\234\223",
 "action"=>"update",
 "id"=>"16",
 "client"=>{"name"=>"Rosie O'Donnell",
            "address"=>{"city"=>"Grand Rapids",
                        "line1"=>"216 Grandville Ave SW",
                        "zip"=>"49503",
                        "line2"=>"",
                        "state_id"=>"1"},
            "phone"=>"",
            "salon_id"=>"1",
            "email"=>""},
 "controll开发者_如何学编程er"=>"clients"}

Can someone please tell me what's going on?


See here: http://guides.rubyonrails.org/2_3_release_notes.html#nested-object-forms (It's a Rails 2.3 article, but it should apply to Rails 3 as well)

In your Client model you need this line:

accepts_nested_attributes_for :address

Also, change the first line of your nested form to this:

<%= f.fields_for :address do |address_form| %>


Assuming you have the following in your clients class:

accepts_nested_attributes_for :address

and

attr_accessible :address_attributes

I would modify the form as follows:

<%= f.fields_for :address do |address_form| %>
  <div class="field">
    <%= address_form.label :line1 %><br />
    <%= address_form.text_field :line1 %>
  </div>
  <div class="field">
    <%= address_form.label :line2 %><br />
    <%= address_form.text_field :line2 %>
  </div>
  <div class="field">
    <%= address_form.label :city %><br />
    <%= address_form.text_field :city %>
  </div>
  <div class="field">
    <%= address_form.label :state_id %><br />
    <%= select("client[address]", "state_id", State.all.collect {|s| [ s.name, s.id ] }) %>       </div>
  <div class="field">
    <%= address_form.label :zip %><br />
    <%= address_form.text_field :zip %>
  </div>
<% end %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜