Ruby-on-rails rails 中的嵌套表单 - 访问 has_many 关系中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2249160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nested forms in rails - accessing attribute in has_many relation
提问by Swamy g
I have a user and a profile model. One user can have many profiles. I need to access only one information from the profiles section (viz the phone number) in my user model during the user creation process. Hence I'm trying to get it done through attr_accessible. My user.rb looks like this.
我有一个用户和一个配置文件模型。一个用户可以拥有多个配置文件。在用户创建过程中,我只需要访问我的用户模型中的配置文件部分(即电话号码)中的一项信息。因此,我试图通过attr_accessible. 我的 user.rb 看起来像这样。
has_many :profiles
attr_accessible :handle, :email, :password, :profile_mobile_number
attr_accessor : :profile_mobile_number
The problem that I'm facing is that when I try to call the getter method profile_mobile_number in a method in user.rb (the method is private, though I think it doesn't matter), I'm getting a null value. I use the following in my users/new.html.erb form
我面临的问题是,当我尝试在 user.rb 中的一个方法中调用 getter 方法 profile_mobile_number(该方法是私有的,但我认为这无关紧要)时,我得到了一个空值。我在我的 users/new.html.erb 表单中使用以下内容
My question is what is the right way to do this? Should I use <% f.fields_for :profile do |ff| -%>or <% f.fields_for :profiles do |ff| -%>(notice that the second one is plural). When I use the plural :profiles, I don't even see the fields on the form. What am I missing here? And what is the tense that needs to be used in model user.rb? :profile_phone_number or :profiles_phone_number? Thanks.
我的问题是这样做的正确方法是什么?我应该使用<% f.fields_for :profile do |ff| -%>或<% f.fields_for :profiles do |ff| -%>(注意第二个是复数)。当我使用复数 :profiles 时,我什至看不到表单上的字段。我在这里缺少什么?模型 user.rb 中需要使用的时态是什么?:profile_phone_number 还是:profiles_phone_number?谢谢。
采纳答案by Pran
You could do something like the following:
您可以执行以下操作:
<% form_for @user, :url => { :action => "update" } do |user_form| %>
...
<% user_form.fields_for :profiles do |profiles_fields| %>
Phone Number: <%= profiles_fields.text_field :profile_mobile_number %>
<% end %>
<% end %>
But since you already have an association, then might as well use 'accepts_nested_attributes_for'
但是既然你已经有了关联,那么不妨使用' accepts_nested_attributes_for'
回答by allenwei
You should watch RailsCasts Nested Model Form.
thanks Ryan Batesgreat work.
您应该观看RailsCasts Nested Model Form。
感谢瑞恩·贝茨 (Ryan Bates) 的出色工作。
回答by ryancheung
http://apidock.com/rails/v3.2.8/ActionView/Helpers/FormHelper/fields_for
http://apidock.com/rails/v3.2.8/ActionView/Helpers/FormHelper/fields_for
This api dock link list many Nested Attributes Examples including one-to-one, one-to-many. It's very helpful!
这个 api 停靠链接列出了许多嵌套属性示例,包括一对一、一对多。这是非常有帮助的!
回答by nanda
You can use 'accepts_nested_attributes_for' to do this; but there's a little trick in forms:
您可以使用“ accepts_nested_attributes_for”来执行此操作;但是在表单上有一个小技巧:
You must use the singular, and call fields_for for each profile, like this:
您必须使用单数,并为每个配置文件调用 fields_for,如下所示:
<% form_for @user do |f| -%>
<% @user.profiles.each do %>
<% f.fields_for :profile_attributes, profile do |ff| -%>
<% end %>
Notice that is :profile_attributes, instead of just :profile.
请注意,这是:profile_attributes,而不仅仅是:profile。

