获取fields_for和accepts_nested_attributes_for就用belongs_to的关系工作 [英] Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship

查看:163
本文介绍了获取fields_for和accepts_nested_attributes_for就用belongs_to的关系工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法得到一个嵌套的表格中轨生成查看使用新的一个 belongs_to的关系 accepts_nested_attributes_for 的Rails 2.3的设备。我也查了许多可用的资源,它看起来像我的code的的合作,但 fields_for 爆炸在我身上,我怀疑它是与我怎么也配置了嵌套模型。

I cannot seem to get a nested form to generate in a rails view for a belongs_to relationship using the new accepts_nested_attributes_for facility of Rails 2.3. I did check out many of the resources available and it looks like my code should be working, but fields_for explodes on me, and I suspect that it has something to do with how I have the nested models configured.

我打的错误是常见的一个可以有许多原因:

The error I hit is a common one that can have many causes:

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

下面是所涉及的两个型号:

Here are the two models involved:

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

也许这就是我这样做荣,作为一个帐户可以有一个所有者,并可用户,但用户只有一个账户,根据用户模型ACCOUNT_ID键。

Perhaps this is where I am doing it 'rong', as an Account can have an 'owner', and may 'users', but a user only has one 'account', based on the user model account_id key.

这是在new.html.haml视图code我的,打击了:

This is the view code in new.html.haml that blows up on me:

- form_for :account, :url => account_path do |account|
  = account.text_field :name
  - account.fields_for :owner do |owner|
    = owner.text_field :name

这是控制器code新动作:

And this is the controller code for the new action:

class AccountsController < ApplicationController
  # GET /account/new
  def new
    @account  = Account.new
  end
end

当我尝试加载/帐号/新我得到了以下异常:

When I try to load /account/new I get the following 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

如果我尝试使用神秘的建设的方法,它只是弹了控制器,也许是因为建设仅仅是多记录的关系:

If I try to use the mysterious 'build' method, it just bombs out in the controller, perhaps because build is just for multi-record relationships:

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 [车主]不允许作为一个实例变量的名字。

If I try to set this up using @account.owner_attributes = {} in the controller, or @account.owner = User.new, I'm back to the original error, "@account[owner] is not allowed as an instance variable name".

有没有人有新的accepts_nested_attributes_for方法有belongs_to的关系的工作?是不是有什么特别的或不同的,你要干什么?所有正式示例和示例code(像的在Ryans聊天)伟大的东西在涉及多记录关联。

Does anybody else have the new accepts_nested_attributes_for method working with a belongs_to relationship? Is there something special or different you have to do? All the official examples and sample code (like the great stuff over at Ryans Scraps) is concerned with multi-record associations.

推荐答案

我觉得你的 accepts_nested_attributes 是关系的错误的一边。也许像这样的工作吗?

I think your accepts_nested_attributes is on the wrong side of the relationship. Maybe something like this would work?

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的帐户。

For building the account you want to use build_account.

您可以看到更多的例子的文档

You can see more examples in the docs.

这篇关于获取fields_for和accepts_nested_attributes_for就用belongs_to的关系工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆