Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle multiple models in a rails form

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 are nested. what if they are not? lets say, I have a form, where the user fills personal details, address details and a bunch of checkboxes specifying her interests. There are at least 3 tables involved in this one single form, what is the best way to handle this, without having 3 different save buttons?

like image 443
user85748 Avatar asked May 21 '09 12:05

user85748


1 Answers

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.

like image 154
Brian Hogan Avatar answered Sep 21 '22 05:09

Brian Hogan