获取fields_for和accepts_nested_attributes_for以使用belongs_to关系
我似乎无法使用Rails 2.3的新的accepts_nested_attributes_for
工具在rails视图中为belongs_to
关系生成嵌套表单。 我确实检查了许多可用的资源,看起来我的代码应该正常工作,但fields_for
爆炸了我,我怀疑它与我如何配置嵌套模型有关。
我遇到的错误是一个常见的错误原因:
'@account[owner]' is not allowed as an instance variable name
以下是涉及的两个模型:
class Account 'User', :foreign_key => 'owner_id' accepts_nested_attributes_for :owner has_many :users end class User < ActiveRecord::Base belongs_to :account end
也许这就是我在做’rong’的地方,因为帐户可以拥有’所有者’,可能是’用户’,但用户只有一个’帐户’,基于用户模型account_id键。
这是new.html.haml中的视图代码,它炸毁了我:
- form_for :account, :url => account_path do |account| = account.text_field :name - account.fields_for :owner do |owner| = owner.text_field :name
这是新操作的控制器代码:
class AccountsController < ApplicationController # GET /account/new def new @account = Account.new end end
当我尝试加载/ account / new时,我得到以下exception:
NameError in Accounts#new Showing app/views/accounts/new.html.haml where line #63 raised: @account[owner] is not allowed as an instance variable name
如果我尝试使用神秘的“构建”方法,它只会在控制器中爆炸,也许是因为构建仅适用于多记录关系:
class AccountsController < ApplicationController # GET /account/new def new @account = Account.new @account.owner.build end end You have a nil object when you didn't expect it! The error occurred while evaluating nil.build
如果我尝试在控制器中使用@ account.owner_attributes = {}来设置它,或者@ account.owner = User.new,我又回到了原始错误,“@account [owner]不允许作为实例变量名”。
是否有其他人使用belongs_to关系使用新的accepts_nested_attributes_for方法? 你有什么特别或不同的东西吗? 所有官方示例和示例代码(如Ryans Scraps上的精彩内容 )都涉及多记录关联。
我认为你的accepts_nested_attributes
在关系的错误方面。 也许这样的事情可行吗?
class Account < ActiveRecord::Base belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id' has_many :users end class User < ActiveRecord::Base belongs_to :account has_one :account, :foreign_key => :owner_id accepts_nested_attributes_for :account end
要构建您要使用build_account的帐户。
您可以在文档中看到更多示例。
我已经有几个月太晚了,但是我想解决这个错误,我的情况是我无法改变这种关系,以“面对其他方式”。
答案非常简单,您必须在新的操作中执行此操作:
@account.build_owner
使用fields_for不显示表单的原因是因为它没有有效的对象。 你有正确的想法:
@account.owner.build
但是,这不是belongs_to
工作的方式。 此方法仅使用has_many
和has_and_belongs_to_many
生成。
参考: http : //guides.rubyonrails.org/association_basics.html#belongs-to-association-reference
我正在使用Rails 2.3.5,我注意到了同样的事情。 检查active_record的nested_attributes.rb的源代码,看起来belongs_to应该可以正常工作。 所以它似乎可能是一个“嵌套表单”错误。
我有一个与你的完全一样的嵌套表单, User belongs_to :address
, User belongs_to :address
和Address
独立于用户。
然后在表单中,我只是做<% f.fields_for :address_attributes do |address_form| %>
<% f.fields_for :address_attributes do |address_form| %>
而不是<% f.fields_for :address do |address_form| %>
<% f.fields_for :address do |address_form| %>
。 暂时破解,直到有更好的方式,但这是有效的。 accepts_nested_attributes_for
方法期望params包括如下内容:
{user=>{address_attributes=>{attr1=>'one',attr2=>'two'}, name=>'myname'}
…但fields_for
正在产生:
{user=>{address=>{attr1=>'one',attr2=>'two'}, name=>'myname'}
这样您就不has_one :account
添加到您的代码中,这在我的情况下不起作用。
更新:找到一个更好的答案:
以下是我正在使用的代码的要点:
使用belongs_to Gist Rails嵌套表单
希望有所帮助。
class Account < ActiveRecord::Base belongs_to :owner :class_name => 'User', :foreign_key => 'owner_id' end
这个对我有用。 :foreign_key =>’owner_id’是我案例中的关键问题。