繁体   English   中英

具有多态关联的嵌套 model forms

[英]Nested model forms with polymorphic association

我在嵌套 forms 时遇到了更多麻烦。 这次是我的多态地址 model。 任何帮助将不胜感激。

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

当尝试通过 /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

问题是因为当您将此 model 的地址关联定义为has_one时,您正在执行location.addresses.build 因此,您需要执行location.build_address 如果您曾经想建立一个belongs_to关联,同样的事情也会发生。

不可能在address关联本身上调用build方法,因为address方法将尝试加载关联的 object 并在不能时返回nil ,并且不允许在nil上调用build 因此,您需要执行build_address

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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