简体   繁体   中英

Rails 3 - build_association not working in fields_for within fields_for

I have models set up as follows:

call.rb

belongs_to :contact

contact.rb

has_many :calls
belongs_to :postal_address, class_name: "Address", foreign_key: "postal_address_id"
belongs_to :physical_address, class_name: "Address", foreign_key: "physical_address_id"

address.rb

has_many :contacts

On my contact new/edit forms, I use @contact.build_postal_address and @contact.build_physical_address in the ContactsController which act as expected. The views rendered appear with blank fields for postal and physical address if required.

When a call is logged, a form is brought up during the call. One of the nested resources on this form allows the operator to edit the contact details from the same page as entering other info about the call. For this form, where contact is part of a nested form, the build functions do not work.

My forms used in the ContactsController are as follows:

_contact_form.html.erb

<%= simple_form_for @contact do |f| %>
  <%= f.input :name %>
  <%= render 'address_fields', f: f, fields: :postal %>
  <%= render 'address_fields', f: f, fields: :physical %>
  <%= f.submit %>
<% end %>

_address_fields.html.erb

<%= f.simple_fields_for fields do |a| %>
  <%= a.input :address_line_1 %>
<% end %>

My forms used in the CallsController are as follows (re-using the _address_fields partial:)

_call_form.html.erb

<%= simple_nested_form_for @call do |f| %>
  <%= f.input :call_comments %>
  <%= f.simple_fields_for :contact do |contact| %>
    <%= contact.input :name %>
    <%= render 'address_fields', f: contact, fields: :postal %>
    <%= render 'address_fields', f: contact, fields: :physical %>
  <% end %>
  <%= f.submit %>
<% end %>

No matter what I do with @contact.build_physical_address or @contact.build_postal_address in the controller, the postal and physical address fields do not appear in the call form unless the address already exists under the contact. If the address does already exist, calling the build_* action on postal/physical does not clear the fields, either.

Turns out it was a problem with how I was calling simple_fields_for within the call form.

In the calls controller, I had to set the following:

calls_controller.rb

@contact = params[:contact_id]
@contact.build_postal_address if @contact.postal_address == nil
@contact.build_physical_address if @contact.physical_address == nil    
@call = Call.new(contact_id: @contact.id)

In my call form, I then needed to ammend the fields_for line for contact to be:

_call_form.html.erb

...
<%= f.simple_fields_for :contact, @contact do |contact| %>
...

This caused the @contact object to be used for the fields_for values, instead of the @call.contact object which hadn't been affected by the build_* methods in the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM