2
我陷入了嵌套屬性的麻煩。未定義的方法`build_users'與嵌套模型
這裏是我的帳號模式:
class Account < ActiveRecord::Base
has_many :products
has_many :blogs
has_many :openings
has_many :users
has_one :logo, :class_name => "AccountPicture"
has_one :address, :class_name => "AccountAddress"
has_and_belongs_to_many :options
accepts_nested_attributes_for :logo, :allow_destroy => true
accepts_nested_attributes_for :address, :allow_destroy => true
accepts_nested_attributes_for :users, :allow_destroy => true
end
這裏是我的用戶模型:
class User < ActiveRecord::Base
belongs_to :account
end
爲標誌,地址和用戶正如你所看到的,客戶接受嵌套屬性。
在測試時,我可以使用徽標和地址的嵌套屬性,但不能爲用戶使用。
a = Account.new
=> #<Account id: nil, hostname: nil, subdomain: nil, name: nil, description: nil, base_line: nil, footer: nil, phone_number: nil, mobile_number: nil, email_address: nil, created_at: nil, updated_at: nil>
# building the address works fine
>> a.build_address
=> #<AccountAddress id: nil, account_id: nil, country: nil, state: nil, county: nil, city: nil, suburb: nil, zipcode: nil, street: nil, streetno: nil, longitude: nil, latitude: nil, error_code: nil>
# building the users fails
>> a.build_users
NoMethodError: undefined method `build_users' for #<Account:0x7f6862a5f948>
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in `method_missing'
from (irb):2
因此,我的看法,當我使用嵌套形式,我得到這個錯誤回:
User(#69850615730460) expected, got Array(#69850664775200)
任何幫助,將不勝感激。
謝謝。
編輯:鑑於
<%= error_messages_for :account %>
<% form_for :account, :url => accounts_path do |f| -%>
<fieldset>
<legend>Account</legend>
<%= label_tag 'name' %>
<%= f.text_field :name, :class => "textbox" %>
</fielset>
<% f.fields_for :users do |user_form| %>
<fieldset>
<legend>User</legend>
<%= label_tag 'email' %>
<%= user_form.text_field :email, :class => "textbox" %>
<%= label_tag 'password' %>
<%= user_form.password_field :password, :class => "textbox" %>
<%= label_tag 'password_confirmation' %>
<%= user_form.password_field :password_confirmation, :class => "textbox" %>
</fieldset>
<% end -%>
<div class="buttons">
<%= submit_tag 'Submit', :class => "button" %>
</div>
<% end -%>
請您發表您的觀點一些代碼? – 2010-04-28 16:09:56
你在視圖中看到這個錯誤嗎?這不是說別的嗎? – 2010-04-29 12:25:21