4

I am having some issues with nested models in a form, using Rails 3.1rc4.

I presently have models that look like this:

class Sale < ActiveRecord::Base
  attr_accessible :customer_id, :vehicle_id, :sale_date

  belongs_to :customer
  accepts_nested_attributes_for :customer
end

and

class Customer < ActiveRecord::Base
  attr_accessible :dealership_id, :first_name, :last_name, :address1, :email

  belongs_to :dealership

  has_many :sales
  has_many :vehicles, :through => :sales

end

I've obviously truncated these slightly, but all the important info is there.

I am attempting to set up a sale form that will also allow me to create a new customer, hence the accepts_nested_attributes_for :customer line in the sale model.

My form view looks like (again truncated, only the important part):

    <%= form_for @sale, :html => {:class => 'fullform'} do |f| %>

        <%= f.error_messages %>

        <%= field_set_tag 'Customer Details' do %>
            <% f.fields_for :customer do |builder| %>
                <%= builder.label :first_name %><br>
                <%= builder.text_field :first_name %>
            <% end %>
        <% end %>
    <% end %>

The problem I am having is that neither the text field nor the label for :first_name are showing up when the form is rendered - there is no error message, it just doesn't appear.

I should mention that I have tried both with and without @sale.customer.build in the new method of my controller, but it seems to have had no effect.

Thanks!

Can anyone suggest what I am doing wrong?

EDIT: For the avoidance of doubt, my sales controller's new method looks like:

def new
  @sale = Sale.new
  @sale.customer.build
end
Harry
  • 4,660
  • 7
  • 37
  • 65
  • OK, i've found some info indicating that instead of `@sale.customer.build` I should have `@sale.build_customer`. I have modified the new method to reflect this, but still to no avail. – Harry Jul 15 '11 at 09:10
  • I've tested out the form for editing a Sale record, rather than creating a new one, and I have found that it works fine - as such, the problem is evidently with creating the empty customer record in the controller. Having tried both `@sale.customer.build` and `@sale.build_customer` with neither working, can anyone suggest what I need to have in the controller? – Harry Jul 15 '11 at 10:55

1 Answers1

4

Add customer_attributes to your attr_accessible in the Sale model.

Another mistake; Replace:

<% f.fields_for :customer do |builder| %>

With:

<%= f.fields_for :customer do |builder| %>
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • This will only show the nested fields if the sale has associated customers. To get fields for a new customer you can build new customers in the controller. Check out this railscast and the subsequent episode which show how to add new customer fields through javascript. http://railscasts.com/episodes/196-nested-model-form-part-1 – Wizard of Ogz Jul 15 '11 at 04:19
  • As mentioned, I have already tried this with `@sale.customer.build` (after `@sale = Sale.new`) in the new method of my controller, which does not appear to have any effect. I have also made the adjustments mentioned above (added `:customer_attributes` to attr_accessible, and the missing `=`), but it has had no effect. – Harry Jul 15 '11 at 08:45
  • you should definitely keep `@sale.customer.build` in your code (you mentioned it, that's why I didn't repeat). Do you have any error message? Or could you post the params? – apneadiving Jul 15 '11 at 09:13
  • Just made an edit above - it seems that I should be using `@sale.build_customer` because the sale belongs_to customer? There is still no error message, the fieldset in which the customer fields should appear is simply empty, nothing is rendered. – Harry Jul 15 '11 at 09:18
  • This is actually working - due to an error on my part, I had put the `@sale.build_customer` line in the wrong controller action. Once moved, it now works. Thanks! – Harry Jul 16 '11 at 16:35
  • The damn equal sign caused me so much pain. Thanks for this answer. – Catfish Dec 01 '12 at 03:56