Doppeltes geschachteltes Modell Formen
Ich habe noch immer Probleme mit verschachtelten Formen. Hier ist mein form-code:
<%= 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 %>
Konto Modell:
class Account < ActiveRecord::Base
has_many :organizations
accepts_nested_attributes_for :organizations
end
Organisationsmodell:
class Organization < ActiveRecord::Base
belongs_to :account
has_many :locations
accepts_nested_attributes_for :locations
end
Und die Lage Modell:
class Location < ActiveRecord::Base
belongs_to :organization
end
Und schließlich die Konten Controller:
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
Hier ist die Fehlermeldung die ich erhalte:
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"}
Jede Hilfe für diese wird sehr geschätzt werden. Ich schaue mir das jetzt seit ein paar Stunden.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Sollten Sie verwenden, neue builder in verschachtelter form für verschachtelte verschachtelten form :)) :
Einen noch Trockner-Lösung:
1) im Gemfile, Liste
nested_form
als Abhängigkeit.2) in Ihren Modellen tun:
3) regelmäßige erstellen von Formularen für die Organisation unter
./app/view/organizations/
./app/view/Orte/`and for Location under
4) in Ihrem Konto form, dies zu tun: (es kommt ziemlich kurz Weg!)
5) in Ihrer Organisation zu bilden, dies zu tun: (auch relativ kurzen)
Überprüfen Sie diese RailsCasts:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2