I am trying to setup a simple nested model form bu开发者_如何学运维t I am getting an error when trying to display the form via the 'new' action. Here is my setup:
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 %>
When attempting to hit the 'new' action at /accounts/new I am getting the following error:
uninitialized constant Account::Organization
Application Trace: app/controllers/accounts_controller.rb:5:in 'new'
Any help will be greatly appreciated.
This appears to be an odd load order problem. Are you doing anything clever with config.load_paths or anything like that?
Just for the sake of seeing if it makes it work, try require File.join(Rails.root, 'app/models/organization.rb')
at the top of account.rb. This is not a solution you want to keep around, but if it works with that line then you'll know the problem is with the loader.
精彩评论