Nested forms in rails - accessing attribute in has_many relation

后端 未结 4 994
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 03:06

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 d

4条回答
  •  -上瘾入骨i
    2020-12-09 03:19

    You can use 'accepts_nested_attributes_for' to do this; but there's a little trick in forms:

    You must use the singular, and call fields_for for each profile, like this:

    <% 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.

提交回复
热议问题