Nested forms in rails - accessing attribute in has_many relation

后端 未结 4 996
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  一生所求
    2020-12-09 03:31

    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'

提交回复
热议问题