Hi guys, I’m new to ruby and rails and I’m working on multi model
forms, specifically 3. I’m using this
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]}) %> |
Can you guys help me out with what i’m missing?