how to handle multiple models in a rails form

后端 未结 2 857
轻奢々
轻奢々 2021-01-30 18:43

http://weblog.rubyonrails.org/2009/1/26/nested-model-forms

This post helped in learning how to handle multiple models in a rails form. It works as long as the models ar

相关标签:
2条回答
  • 2021-01-30 18:57

    Two options:

    First is ActivePresenter which works well for this.

    Second is just to use fields_for:

    <%= form_for @user do |f| %>
    
       <%=f.label :name %>
       <%=f.text_field :name %>
    
       <%= fields_for @address do |fa| %>
          <%=fa.label :city %>
          <%=fa.text_field :city %>
       <% end %>
    
    <% end %>
    

    Then in the controller, save the records.

     @user = User.new(params[:user]) 
     @address = Address.new(params[:address])
    

    ActivePresenter works so well though.

    Also found a railsforum post via Google, which would work well.

    0 讨论(0)
  • 2021-01-30 19:14

    You can refer this tutorial by The Pragmatic Programmers

    Advanced Rails Recipes

    0 讨论(0)
提交回复
热议问题