Multi-model forms

Hi guys, I’m new to ruby and rails and I’m working on multi model
forms, specifically 3. I’m using this
http://guides.rubyonrails.org/getting_started.html
as a start, and its got a 2 Model example but I cant seem to get the
last one working.

These are my models:

Country name:string code:string (has_one :address)
Address address_line1:string address_line2:string city:string
state:string country:references (belongs_to :country, has_one :guest)
Guest name:string last_name:string gender:boolean vip:boolean
address:references (belongs_to :address)

So basically what I want to do is insert a new guest through guests/
new and insert a row for each model, but the country is handled
through a select_tag instead of inserting a new country.

This is the guest form, with the nested address form view:
<% if @guest.address.nil? %>
<% @guest.build_address %>
<% end %>
<%= form_for(@guest) do |f| %>
<% if @guest.errors.any? %>


<%= pluralize(@guest.errors.count, “error”) %> prohibited
this guest from being saved:

  <ul>
  <% @guest.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>

<% end %>

<%= render :partial => 'address/form',
         :locals => {:form => f} %>

  <tr>
    <td style="text-align: right; height: 60px; vertical-align:

bottom;"><%= f.submit %>

<%= f.label :name %>
<%= f.text_field :name, :class => 'textfield' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, :class => 'textfield' %>
<%= f.label "Gender" %>
<%= f.radio_button(:gender, "true", :checked => 'checked') %><%= f.label(:gender, "M") %><%= f.radio_button(:gender, "false") %>< %= f.label(:gender, "F") %>
<%= f.label :identification, 'Identification:' %>
<%= f.text_field :identification, :class => 'textfield' %>

<% end %>

This is the nested address form (note select_tag is created with a map
of country from the guests controller):
<%= form.fields_for :address do |address_form| %>

<%= address_form.label :address_line1, 'Address:' %>
<%= address_form.text_field :address_line1, :class => 'textfield' %>
<%= address_form.text_field :address_line2, :class => 'textfield' %>
<%= address_form.label :city, 'City:' %>
<%= address_form.text_field :city, :class => 'textfield' %>
<%= address_form.label :state, 'State:' %>
<%= address_form.text_field :state, :class => 'textfield' %>
<%= address_form.label :postal_code, 'Postal Code:' %>
<%= address_form.text_field :postal_code, :class => 'textfield' %>
<%= address_form.label :country %> <%= address_form.select(:country, @countries.map {|u| [u.name,u.id]}) %>
<% unless address_form.object.nil? || address_form.object.new_record? %>
<%= address_form.label :_destroy, 'Remove:' %> <%= address_form.check_box :_destroy %>
<% end %> <% end %>

Can you guys help me out with what i’m missing?

http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

Thanks for the links. That tutorial is great, but it doesn’t answer my
question, I want to be able to select an option of the third model
(country) and not insert a new country everytime a new guest is
created.

On Mon, Mar 14, 2011 at 9:33 PM, robvelor
[email protected]wrote:

Thanks for the links. That tutorial is great, but it doesn’t answer my
question, I want to be able to select an option of the third model
(country) and not insert a new country everytime a new guest is
created.

oh i see you just want to use collection select to be able to choose a
country, here , read about collection select

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

if you are still confused try using simple_form it has a country select
plugin

https://github.com/plataformatec/simple_form

Thanks got it working :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs