Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:07:32+00:00 2026-05-24T14:07:32+00:00

I am having more trouble with nested forms. This time with my polymorphic Address

  • 0

I am having more trouble with nested forms. This time with my polymorphic Address model. Any help will be greatly appreciated.

<%= form_for @account do |f| %>

<%= f.label :account_type %><br />
<%= f.text_field :account_type %><br />

    <%= f.fields_for :organizations do |builder| %>
        <%= builder.label :name %><br />
        <%= builder.text_field :name %><br />
        <%= builder.label :website %><br />
        <%= builder.text_field :website %><br />

        <%= builder.fields_for :locations do |lb| %>
            <%= lb.label :phone %><br />
            <%= lb.text_field :phone %><br />
            <%= lb.label :toll_free_phone %><br />
            <%= lb.text_field :toll_free_phone %><br />
            <%= lb.label :fax %><br />
            <%= lb.text_field :fax %><br />

            <%= lb.fields_for :address do |ab| %>
                <%= ab.label :address1 %><br />
                <%= ab.text_field :address1 %><br />
                <%= ab.label :address2 %><br />
                <%= ab.text_field :address2 %><br />
                <%= ab.label :city %><br />
                <%= ab.text_field :city %><br />
                <%= ab.label :state %><br />
                <%= ab.text_field :state %><br />
                <%= ab.label :zip %><br />
                <%= ab.text_field :zip %><br />
            <% end %>
        <% end %>
    <% end %>

<%= f.submit "Add account" %>
<% end %>

class Account < ActiveRecord::Base

    has_many :organizations

    accepts_nested_attributes_for :organizations
end

class Organization < ActiveRecord::Base

    belongs_to :account

    has_many :locations

    accepts_nested_attributes_for :locations
end

class Location < ActiveRecord::Base

    belongs_to :organization

    has_one :address, :as => :addressable

end

class Address < ActiveRecord::Base

    belongs_to :addressable, :polymorphic => true
end

class AccountsController < ApplicationController

def new
    @account = Account.new
    organization = account.organizations.build
    location = organization.locations.build
    location.addresses.build

    @header = "Create account"
end

def create
    @account = Account.new(params[:account])
    if @account.save
        #handle success
    else
        render 'new'
    end
end
end

When trying to display the form via /accounts/new I am getting the following error message:

NoMethodError in AccountsController#new

undefined method `addresses' for #<Location:0x18d7718>
Rails.root: C:/Documents and Settings/Corey Quillen/My     
Documents/rails_projects/shop_manager

Application Trace | Framework Trace | Full Trace
app/controllers/accounts_controller.rb:7:in `new'
Request

Parameters:

None
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:07:33+00:00Added an answer on May 24, 2026 at 2:07 pm

    The problem is because you’re doing location.addresses.build when you’ve defined the Address association for this model as a has_one. As such, you will need to do location.build_address. The same thing goes for if you were ever looking to build a belongs_to association.

    It is not possible for the build method to be called on the address association itself, as the address method would attempt to load the associated object and return nil if it can’t, and calling build on nil is just not allowed. Therefore, you need to do build_address.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble wrapping my head around more advanced Rails query methods. In this
I'm having trouble looking for information in this topic, when it's more efficient to
I've having trouble with nested while loops. I've created this function to list three
I'm having trouble wrapping my head around this issue. I have a business model
I'm having trouble with Timeouts which do not work reliably under JRuby (Linux). More
Having trouble with a query to return the newest order of any grouped set
I am having trouble understanding the meaning and more importantly the concept of an
I'm having some trouble binding to a nested list with the default binder. I
I am having more trouble with rendering 3d objects in java, heres my new
Im having a bit of trouble understanding the more complex system calls in assembly.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.