/ / Verschachtelte Modellformen - Rubin auf Schienen, Rubin auf Schienen-3

Verschachtelte Modellformen - Ruby-on-Rails, Ruby-on-Rails-3

Ich versuche, ein einfaches verschachteltes Modellformular einzurichten, es wird jedoch eine Fehlermeldung angezeigt, wenn versucht wird, das Formular über die Aktion "neu" anzuzeigen. Hier ist mein 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 %>

Beim Versuch, die "neue" Aktion unter / accounts / new zu treffen, erhalte ich die folgende Fehlermeldung:

nicht initialisiertes konstantes Konto :: Organisation

Anwendungs-Trace: app / controller / accounts_controller.rb: 5: in "neu"

Jede Hilfe wird sehr geschätzt.

Antworten:

0 für die Antwort № 1

Dies scheint ein ungerades Problem der Ladereihenfolge zu sein. Machen Sie etwas cleveres mit config.load_paths oder so etwas?

Nur um zu sehen, ob es funktioniert, versuchen Sie es require File.join(Rails.root, "app/models/organization.rb") an der Spitze von account.rb. Dies ist keine Lösung, die Sie beibehalten möchten, aber wenn es mit dieser Linie funktioniert, wissen Sie, dass das Problem beim Lader liegt.