gpt4 book ai didi

ruby-on-rails-3 - Accepts_nested_attributes_for 是否可以与Belongs_to一起使用?

转载 作者:行者123 更新时间:2023-12-03 00:57:39 24 4
gpt4 key购买 nike

关于这个基本问题,我得到了各种相互矛盾的信息,而答案对于我当前的问题至关重要。那么,很简单,在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

在 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"

最佳答案

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

关于ruby-on-rails-3 - Accepts_nested_attributes_for 是否可以与Belongs_to一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7365895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com