Hi guys, I'm new to ruby and rails and I'm working on multi model forms, specifically 3. I'm using this Getting Started with Rails — Ruby on Rails Guides 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? %> <div id="error_explanation"> <h2><%= pluralize(@guest.errors.count, "error") %> prohibited this guest from being saved:</h2>
<ul> <% @guest.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
<table> <tr><td><%= f.label :name %></td></tr> <tr> <td><%= f.text_field :name, :class => 'textfield' %></td> </tr> <tr><td><%= f.label :last_name %></td></tr> <tr> <td><%= f.text_field :last_name, :class => 'textfield' %></td> </tr> <tr><td><%= f.label "Gender" %></td></tr> <tr> <td><%= f.radio_button(:gender, "true", :checked => 'checked') %><%= f.label(:gender, "M") %><%= f.radio_button(:gender, "false") %>< %= f.label(:gender, "F") %></td> </tr> <tr><td><%= f.label :identification, 'Identification:' %></td></
<tr> <td><%= f.text_field :identification, :class => 'textfield' %></td> </tr>
<%= render :partial => 'address/form', :locals => {:form => f} %>
<tr> <td style="text-align: right; height: 60px; vertical-align: bottom;"><%= f.submit %></td> </tr> </table>
<% 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| %>
<table> <tr><td><%= address_form.label :address_line1, 'Address:' %></
</tr>
<tr> <td><%= address_form.text_field :address_line1, :class => 'textfield' %></td> </tr> <tr> <td><%= address_form.text_field :address_line2, :class => 'textfield' %></td> </tr> <tr><td><%= address_form.label :city, 'City:' %></td></tr> <tr> <td><%= address_form.text_field :city, :class => 'textfield' %></td> </tr> <tr><td><%= address_form.label :state, 'State:' %></td></tr> <tr> <td><%= address_form.text_field :state, :class => 'textfield' %></td> </tr> <tr><td><%= address_form.label :postal_code, 'Postal Code:' %></
</tr>
<tr> <td><%= address_form.text_field :postal_code, :class => 'textfield' %></td> </tr> <tr> <td><%= address_form.label :country %></td> <td><%= address_form.select(:country, @countries.map {|u| [u.name,u.id]}) %></td> </tr> </table> <% unless address_form.object.nil? || address_form.object.new_record? %> <div class="field"> <%= address_form.label :_destroy, 'Remove:' %> <%= address_form.check_box :_destroy %> </div> <% end %> <% end %>
Can you guys help me out with what i'm missing?