当前位置:  开发笔记 > 编程语言 > 正文

获取fields_for和accepts_nested_attributes_for以使用belongs_to关系

如何解决《获取fields_for和accepts_nested_attributes_for以使用belongs_to关系》经验,为你挑选了3个好方法。

我似乎无法belongs_to使用accepts_nested_attributes_forRails 2.3 的新工具在rails视图中生成嵌套表单以获取关系.我确实检查了许多可用的资源,看起来我的代码应该正常工作,但是fields_for爆炸对我来说,我怀疑它与我如何配置嵌套模型有关.

我遇到的错误是一个常见的错误,可能有很多原因:

'@account[owner]' is not allowed as an instance variable name

以下是涉及的两个模型:

class Account < ActiveRecord::Base
  # Relationships
  belongs_to :owner, :class_name => '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时,我得到以下异常:

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上的精彩内容)都与多记录关联有关.



1> Jaryl..:

我已经来不及几个月了,但是我想解决这个错误,而我的情况是我无法改变这种关系,以"面对其他方式".

答案非常简单,您必须在新的操作中执行此操作:

@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



2> Andy Gaskell..:

我认为你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的帐户.

您可以在文档中看到更多示例.



3> Lance Pollar..:

I'm using Rails 2.3.5 and I noticed the same thing. Checking out the source for active_record's nested_attributes.rb, it looks like belongs_to should work fine. So it appears it might be a "nested forms" bug.

I have a nested form exactly like yours, with User belongs_to :address, and Address is independent of the user.

Then in the form, I just do <% f.fields_for :address_attributes do |address_form| %> instead of <% f.fields_for :address do |address_form| %>. Temporary hack until there's a better way, but this works. The method accepts_nested_attributes_for is expecting the params to include something like:

{user=>{address_attributes=>{attr1=>'one',attr2=>'two'}, name=>'myname'}

...but fields_for is producing:

{user=>{address=>{attr1=>'one',attr2=>'two'}, name=>'myname'}

This way you don't have to add that has_one :account to your code, which doesn't work in my case.

Update: Found a better answer:

Here is the gist of the code I'm using to make this work right:

Rails Nested Forms with belongs_to Gist

Hope that helps.

推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有