【问题标题】:Double nested model forms双嵌套模型形式
【发布时间】:2011-08-10 04:48:06
【问题描述】:

我仍然遇到嵌套表单的问题。这是我的表单代码:

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

        <%= f.fields_for :locations do |builder| %>
            <%= builder.label :phone %><br />
            <%= builder.text_field :phone %><br />
            <%= builder.label :toll_free_phone %><br />
            <%= builder.text_field :toll_free_phone %><br />
            <%= builder.label :fax %><br />
            <%= builder.text_field :fax %><br />
        <% 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

end

最后是帐户控制器:

class AccountsController < ApplicationController

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

    @header = "Create account"
end

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

这是我得到的错误:

ActiveRecord::UnknownAttributeError in AccountsController#create

unknown attribute: locations
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:12:in `new'
app/controllers/accounts_controller.rb:12:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"xuZLSP+PSjdra3v51nIkJYZeXRM0X88iF135hPlp4sc=",
 "account"=>{"account_type"=>"Company",
 "organizations_attributes"=>{"0"=>{"name"=>"Atlas",
 "website"=>"www.atlas.com"}},
 "locations"=>{"phone"=>"555-555-5555",
 "toll_free_phone"=>"800-555-5555",
 "fax"=>"512-555-5555"}},
 "commit"=>"Add account"}

对此的任何帮助将不胜感激。我已经看了几个小时了。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms


    【解决方案1】:

    对于嵌套的嵌套形式,您应该使用嵌套形式的新构建器:)) :

    <%= 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 />
            <% end %>
        <% end %>
    
    <%= f.submit "Add account" %>
    <% end %>
    

    【讨论】:

      【解决方案2】:

      更均匀的 DRYer 解决方案:

      1) 在您的 Gemfile 中,将 nested_form 列为依赖项。

      2) 在你的模型中这样做:

      class Account <  ActiveRecord::Base
        accepts_nested_attributes_for :organizations
      end
      
      class Organization < ActiveRecord::Base
        accepts_nested_attributes_for :locations
      end
      

      3) 在./app/view/organizations/ and for Location under./app/view/locations/`下为组织创建常规表单

      4) 在您的帐户表单中,执行以下操作:(这样会变得很短!)

      <%= nested_form_for @account do |f| %>
      
      <%= f.label :account_type %><br />
      <%= f.text_field :account_type %><br />
      
       <%= f.fields_for :organizations %>
      
      <%= f.submit "Add account" %>
      <% end %>
      

      5) 在您的组织表单中,执行以下操作:(也很短)

      <%= nested_form_for @organization do |f| %>
      <%= f.label :name %><br />
          <%= f.text_field :name %><br />
          <%= f.label :website %><br />
          <%= f.text_field :website %><br />
      
          <%= f.fields_for :locations %>
      
      <%= f.submit "Add Organization" %>
      <% end %>
      

      检查这些 RailsCasts:

      http://railscasts.com/episodes/196-nested-model-form-part-1

      http://railscasts.com/episodes/197-nested-model-form-part-2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-03
        相关资源
        最近更新 更多