I tried and tried and tried, but I still fail in creating a form with
proper validation and stuff for foreign keys… So I’d really like to
get some help here.
I’m creating a booking site for DJ’s, and for every booking one can
choose one of different countries:
class Booking < ActiveRecord::Base
validates_numericality_of :country_id # Necessary? Correct???
:dependent => true
class Country < ActiveRecord::Base
Now I have created a scaffold of the bookings, but sadly this does not
include the wanted select box for country selection. So I added it
<%= error_messages_for ‘booking’ %>
<%= start_form_tag :action => ‘new’ %>
Country: <%= collection_select(:booking, :country, @countries, :id,
:name, :include_blank => true) %>
<%= end_form_tag %>
So far, so good, the countries are well displayed in a select box. But
when I submit the form, I get the following error:
ActiveRecord::AssociationTypeMismatch in BookingsController#new
Country expected, got String
Application Trace | Framework Trace | Full Trace
Can anybody tell me what to do? Do I have to add some sort of
before_validation method that creates the Country object instead of the
String (which is the ID of the selected country)?
Thanks very much for help.