3

I am using accepts_nested_attributes_for with the has_one polymorphic model in rails 2.3.5 Following are the models and its associations:

class Address < ActiveRecord::Base
  attr_accessible :city, :address1, :address2
  belongs_to :addressable, :polymorphic => true
  validates_presence_of :address1, :address2, :city
end

class Vendor < ActiveRecord::Base
  attr_accessible :name, :address_attributes
  has_one  :address, :as => :addressable, :dependent => :destroy
  accepts_nested_attributes_for :address
end

This is the view:

- form_for @vendor do |f|
  = f.error_messages
  %p
    = f.label :name
    %br
    = f.text_field :name
  - f.fields_for :address_attributes do |address|
    = render "shared/address_fields", :f => address
  %p
    = f.submit "Create"

This is the partial shared/address_fields.html.haml

%p
  = f.label :city
  %br= f.text_field :city
  %span City/Town name like Dharan, Butwal, Kathmandu, ..
%p
  = f.label :address1
  %br= f.text_field :address1
  %span City Street name like Lazimpat, New Road, ..
%p
  = f.label :address2
  %br= f.text_field :address2
  %span Tole, Marg, Chowk name like Pokhrel Tole, Shanti Marg, Pako, ..

And this is the controller: class VendorsController < ApplicationController

  def new
    @vendor = Vendor.new
  end

  def create
    @vendor = Vendor.new(params[:vendor])
    if @vendor.save
      flash[:notice] = "Vendor created successfully!"
      redirect_to @vendor
    else
      render :action => 'new'
    end
  end
end

The problem is when I fill in all the fileds, the record gets save on both tables as expected.

But when I just the name and city or address1 filed, the validation works, error message shown, but the value I put in the city or address1, is not persisted or not displayed inside the address form fields?

This is the same case with edit action too.

Though the record is saved, the address doesn't show up on the edit form. Only the name of the Client model is shown. Actually, when I look at the log, the address model SQL is not queried even at all.

1 Answer 1

1

Why f.fields_for :address_attributes?

Shouldn't it be:

- f.fields_for :address do |address_fields|
  = render "shared/address_fields", :f => address_fields

It's not loading the values on edit and errors because you never load address_attributes with the values from @vendor.address.

Sign up to request clarification or add additional context in comments.

4 Comments

If I just use :address instead of :address_attributes, on the new form, I only see the client name field. The address fields are not shown. So, as shown in RailsCasts epi-196, on the controller it says to create the child as like @vendor.address.build I get this error gist.github.com/384010
And here is the params passed to create method: Processing VendorsController#create (for 127.0.0.1 at 2010-04-30 00:32:03) [POST] Parameters: {"commit"=>"Create", "authenticity_token"=>"5KQpowfKMWCc/FUXWjXnDNL9F/vBnnOXnCkWvXeSFA8=", "vendor"=>{"name"=>"", "address_attributes"=>{"city"=>"Kathmandu", "address1"=>"", "address2"=>""}}}
You want @vendor.build_address (since this is a one-to-one relationship)
Thanks, yay that was the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.