1
我试图设置一个简单的嵌套模型窗体,但在尝试通过“新”操作显示窗体时出现错误。这里是我的设置:嵌套模型表格
class Account < ActiveRecord::Base
has_many :people
has_many :organizations
accepts_nested_attributes_for :organizations
end
class Organization < ActiveRecord::Base
belongs_to :account
has_many :locations
accepts_nested_attributes_for :people
accepts_nested_attributes_for :addresses
end
class AccountsController < ApplicationController
def new
@account = Account.new
@account.organizations.build
end
def create
@account = Account.new(params[:account])
if @account.save
#handle success
else
render 'new'
end
end
end
<%= form_for(@account) do |f| %>
<%= f.label :type %><br />
<%= f.text_field :type %><br />
<%= f.fields_for :organization do |organization_fields| %>
<%= organization_fields.label :name %><br />
<%= organization_fields.text_field :name %><br />
<%= organization_fields.label :website %><br />
<%= organization_fields.text_field :website %><br />
<% end %>
<%= f.submit "Add account" %>
<% end %>
当试图击中/账户的“新”动作/新我收到以下错误:
未初始化的经常项目::组织
应用程序跟踪: app/controllers/accounts_controller.rb:5:in'new'
任何帮助将不胜感激。
我发现我的错误。在我的组织模型中,我添加了accep_nested_attributes_for:locations并且它可以工作。愚蠢的错误。谢谢你的帮助。 –