Accepts_nested_attributes_for 是否可以与Belongs_to一起使用?

发布于 2024-12-04 05:40:06 字数 529 浏览 2 评论 0原文

关于这个基本问题,我得到了各种相互矛盾的信息,而答案对于我当前的问题至关重要。那么,很简单,在Rails 3中,是否允许将accepts_nested_attributes_for与belongs_to关系一起使用?

class User < ActiveRecord::Base
  belongs_to :organization
  accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
  has_many :users
end

在一个视图中:

= form_for @user do |f|
  f.label :name, "Name"
  f.input :name

  = f.fields_for :organization do |o|
    o.label :city, "City"
    o.input :city

  f.submit "Submit"

I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship?

class User < ActiveRecord::Base
  belongs_to :organization
  accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
  has_many :users
end

In a view:

= form_for @user do |f|
  f.label :name, "Name"
  f.input :name

  = f.fields_for :organization do |o|
    o.label :city, "City"
    o.input :city

  f.submit "Submit"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝颜夕 2024-12-11 05:40:06

从 Rails 4 开始,嵌套属性似乎对于“belongs_to”关联工作得很好。它可能在早期版本的 Rails 中发生了更改,但我在 4.0.4 中进行了测试,它确实按预期工作。

Nested attributes appear to work fine for a belongs_to association as of Rails 4. It might have been changed in an earlier version of Rails, but I tested in 4.0.4 and it definitely works as expected.

裂开嘴轻声笑有多痛 2024-12-11 05:40:06

epochwolf 引用的文档在第一行中指出“嵌套属性允许您通过父级在关联记录上保存属性。” (我的重点)。

您可能对这另一个 SO 问题与这个问题相同。它描述了两种可能的解决方案:1)将accepts_nested_attributes移至关系的另一方(在本例中为组织),或2)使用build 方法,用于在呈现表单之前在用户中构建组织。

我还找到了一个要点,描述了如果您愿意的话,将accepts_nested_attributes与belongs_to关系一起使用的潜在解决方案处理一些额外的代码。这也使用 build 方法。

The doc epochwolf cited states in the first line "Nested attributes allow you to save attributes on associated records through the parent." (my emphasis).

You might be interested in this other SO question which is along the same lines as this one. It describes two possible solutions: 1) moving the accepts_nested_attributes to the other side of the relationship (in this case, Organization), or 2) using the build method to build the Organization in the User before rendering the form.

I also found a gist that describes a potential solution for using accepts_nested_attributes with a belongs_to relationship if you're willing to deal with a little extra code. This uses the build method as well.

甜`诱少女 2024-12-11 05:40:06

对于 Rails 3.2 中的 belongs_to 关联,嵌套模型需要以下两个步骤:

(1) 将新的 attr_accessible 添加到您的子模型(用户模型)。

accepts_nested_attributes_for :organization
attr_accessible :organization_attributes

(2) 将@user.build_organization 添加到您的子控制器(用户控制器)以创建列organization

def new
  @user = User.new
  @user.build_organization
end

For belongs_to association in Rails 3.2, nested model needs the following two steps:

(1) Add new attr_accessible to your child-model (User model).

accepts_nested_attributes_for :organization
attr_accessible :organization_attributes

(2) Add @user.build_organization to your child-controller (User controller) in order to create column organization.

def new
  @user = User.new
  @user.build_organization
end
心房敞 2024-12-11 05:40:06

对于 Ruby on Rails 5.2.1

class User < ActiveRecord::Base
  belongs_to :organization
  accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
  has_many :users
end

刚刚到达你的控制器,假设是“users_controller.rb”:

Class UsersController < ApplicationController

    def new
        @user = User.new
        @user.build_organization
    end
end

视图就像 Nick 所做的那样:

= form_for @user do |f|
  f.label :name, "Name"
  f.input :name

  = f.fields_for :organization do |o|
    o.label :city, "City"
    o.input :city

  f.submit "Submit"

最后我们看到 @user3551164 已经解决了,但是现在(Ruby on Rails 5.2.1 )我们不需要 attr_accessible :organization_attributes

For Ruby on Rails 5.2.1

class User < ActiveRecord::Base
  belongs_to :organization
  accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
  has_many :users
end

Just got to your controller, suppose to be "users_controller.rb":

Class UsersController < ApplicationController

    def new
        @user = User.new
        @user.build_organization
    end
end

And the view just as Nick did:

= form_for @user do |f|
  f.label :name, "Name"
  f.input :name

  = f.fields_for :organization do |o|
    o.label :city, "City"
    o.input :city

  f.submit "Submit"

At end we see that @user3551164 have already solved, but now (Ruby on Rails 5.2.1) we don't need the attr_accessible :organization_attributes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文