Ruby on rails 具有多态关联的嵌套模型形式

Ruby on rails 具有多态关联的嵌套模型形式,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我在使用嵌套表单时遇到了更多问题。这次是我的多态地址模型。任何帮助都将不胜感激 <%= form_for @account do |f| %> <%= f.label :account_type %><br /> <%= f.text_field :account_type %><br /> <%= f.fields_for :organizations do |builder| %> <%

我在使用嵌套表单时遇到了更多问题。这次是我的多态地址模型。任何帮助都将不胜感激

<%= 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























类帐户:addressable 结束 类地址true 结束 类AccountsController<应用程序控制器 def新 @account=account.new 组织=account.organizations.build 位置=organization.locations.build location.addresses.build @header=“创建帐户” 结束 def创建 @account=account.new(参数[:account]) 如果@account.save #成功 其他的 呈现“新” 结束 结束 结束
尝试通过/accounts/new显示表单时,我收到以下错误消息:

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
AccountsController中的命名错误#新建
未定义的方法“地址”#
Rails.root:C:/Documents and Settings/Corey Quillen/My
文件/轨道项目/车间经理
应用程序跟踪|框架跟踪|完整跟踪
app/controllers/accounts\u controller.rb:7:in'new'
要求
参数:
没有一个

问题在于,当您将此模型的地址关联定义为
时,您正在执行
location.addresses.build
。因此,您需要执行
位置.生成\u地址
。如果你曾经想建立一个
属于
的关联,同样的事情也会发生


address
关联本身不可能调用
build
方法,因为
address
方法将尝试加载关联对象并返回
nil
,如果不能,则不允许在
nil
上调用
build
。因此,您需要执行
build\u address

问题是,当您将此模型的地址关联定义为
has\u one
时,您正在执行
location.addresses.build
。因此,您需要执行
位置.生成\u地址
。如果你曾经想建立一个
属于
的关联,同样的事情也会发生


address
关联本身不可能调用
build
方法,因为
address
方法将尝试加载关联对象并返回
nil
,如果不能,则不允许在
nil
上调用
build
。因此,在发布问题时,您需要执行
build\u address

,请将您的代码的各个关注点分隔为单独的标题块。这使得潜在的回答者更容易解释您的问题:)在发布问题时,请将代码中的各个问题分隔成单独的标题块。这使得潜在的回答者更容易解释您的问题:)