create table events ( id int not null auto_increment, name varchar(100), city_id int not null, primary key (id) ); class Event < ActiveRecord::Base belongs_to :city end <tr> <td><label for="event_city">City:</label></td> <td><%= select("event","city",City.find_all) %></td> </tr> <select id="event_city" name="event[city]"> <option value="2">New York</option> <option value="1">Boston</option></select> When I submit the form I get following exception "City expected, got String". Do I need to convert the city_id to Integer in controller? Thanks. -=- Neeraj _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Neeraj Kumar wrote:> <select id="event_city" name="event[city]"> > > When I submit the form I get following exception "City expected, got > String".It should be <select id="event_city_id" name="event[city_id]"> Better still, use the ''select'' helper with method parameter :city_id to get Rails to automatically set the initial value. You''ll need to instantiate an @event object to make this happen. -- We develop, watch us RoR, in numbers too big to ignore.
I''m trying to do something similar to this. Basically I have a :belongs_to relationship, and I use collection_select( or simply select( to display these on the form: <p><label for="goal_project">Area | Project</label><br/> <%= select(''goal'', :project_id, Project.projects_for_select, { :prompt => "pick your project"}) %></p> If I use :project_id most things work: validations do the expected, the object is saved as expected. However, when a validation fails, the item is *not* wrapped in the fieldWithErrors div. If I use :project, validations work. The item *is* wrapped in the fieldWithErrors div. However, I get the "Project expected, got String" error. I guess I can load the appropriate Project object in the create action, but I suspect there is something simple that I am overlooking. Thank you. -kdf On 11/5/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> > Neeraj Kumar wrote: > > > <select id="event_city" name="event[city]"> > > > > When I submit the form I get following exception "City expected, got > > String". > > It should be <select id="event_city_id" name="event[city_id]"> > > Better still, use the ''select'' helper with method parameter > :city_id to get Rails to automatically set the initial value. > You''ll need to instantiate an @event object to make this happen. > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Kelly Dwight Felkins wrote:> I''m trying to do something similar to this. Basically I have a > :belongs_to relationship, and I use collection_select( or simply select( > to display these on the form: > > <p><label for="goal_project">Area | Project</label><br/> > <%= select(''goal'', :project_id, Project.projects_for_select, { :prompt > => "pick your project"}) %></p> > > If I use :project_id most things work: validations do the expected, the > object is saved as expected. However, when a validation fails, the item > is *not* wrapped in the fieldWithErrors div. > > If I use :project, validations work. The item *is* wrapped in the > fieldWithErrors div. However, I get the "Project expected, got String" > error.You definitely should be using project_id, but if you use either "validates_associated :project" or "validates_presence_of :project" you''ll have to copy the errors in project to project_id in code in the "validate" method after calling "super". Perhaps Rails should provide an option to automatically copy errors of association attributes to their foreign key attributes. It may be better to validate project_id itself, using either "validate" or "validates_each" to check the nil-ness and integrity of the project attribute. -- We develop, watch us RoR, in numbers too big to ignore.