hello, supposing i have a User class that contains a Wibble class User < ActiveRecord::Base has_one :wibble end in my view i have <%= collection_select :user, :wibble, Wibble.find(:all), :id, :name %> when i post, i get an error like "Wibble expected, got String" how/where am I supposed to convert this posted wibble_id into a Wibble, or am I using collection_select incorrectly? i''ve seen some suggestions to use :wibble_id in the select, e.g. <%= collection_select :user, :wibble_id, Wibble.find(:all), :id, :name %> but if i do this i get an "undefined method wibble_id" error thanks, Jeff
Jeff Emminger wrote:> hello, > > supposing i have a User class that contains a Wibble > > class User < ActiveRecord::Base > has_one :wibble > end > > in my view i have > <%= collection_select :user, :wibble, Wibble.find(:all), :id, :name %> > > when i post, i get an error like > "Wibble expected, got String" > > how/where am I supposed to convert this posted wibble_id into a > Wibble, or am I using collection_select incorrectly? > > i''ve seen some suggestions to use :wibble_id in the select, e.g. > <%= collection_select :user, :wibble_id, Wibble.find(:all), :id, :name > %> > > but if i do this i get an "undefined method wibble_id" error > > thanks, > JeffYou''re trying to get a select list for a form? Here''s how I do it: This line grabs entries where users are tied to groups. GroupUsers belongs to Group, Groups have many GroupUsers. <% @groups = GroupUser.find(:all, :conditions => "user_id=''"+session[:user_id].to_s+"''") %> Now take @groups array and for each entry in it, have it display the name of the group, and then embed the group ID: <%= select("group_users", "group_id", @groups.collect {|p| [ p.group.name, p.group.id ] }, { :include_blank => true }) %> It turns it into this html: <select name="group_users[group_id]"> <option></option> <option value="1">Group1</option> <option value="2">Group2</option> <option value="5">Group5</option> </select> Hope this helps! -Ben Lisbakken -- Posted via http://www.ruby-forum.com/.
ben, thanks for the reply. that''s pretty much what i''m doing... specifically i''m trying to attach my own TimeZoneImpl class to the User. the User has_one :time_zone_impl my select list ends up being semantically identical - time zone name display with the id for each option value. the problem is that when the form is submitted, rails is trying to put the int id in the user.time_zone_impl slot, when it needs to be an instance of TimeZoneImpl instead. perhaps i need to look into composed_of since the User is composed of a TimeZoneImpl?
i think i''ve solved it... using :time_zone_impl_id in the collection_select call is correct. my error was not having association field in the DB spelled correctly! :-D -- Posted via http://www.ruby-forum.com/.
hello gentlemen could u plz give me an example for collection_select rather than mentioning syntax for it. for example lets consider user have to select multiple values like elective subject say it has a subject a,b,c,d,e user can select any number of subject. gentlemen am awaiting for a reply & have a nice day Advance Thx regards narayana -- Posted via http://www.ruby-forum.com/.
<%= select_tag options_from_collection_for_select(@people, "id", "name") %> where @people = Person.find(:all) # or some other collection of variables and where ''id'' and ''name'' are both in the Person model (in the table of people, there is for example: id int auto_increment, name varchar255, [and more columns..] .. ) and this will output: <select> <option value=((the id of the person 1))> the name of the person 1</option> <option value=((the id of the person 2))> the name of the person 2</option> <option value=((the id of the person 3))> the name of the person 3</option> . . . </select> hope it helps, s -- Posted via http://www.ruby-forum.com/.
Narayana Karthik
2006-Jul-19 04:46 UTC
[Rails] Re: Thx to shai for helping Using collection_select
hello shai Thx for helping i know already how to accept multiple values in html format i want to know how to do this with collection_select advance thx have a nice day -- Posted via http://www.ruby-forum.com/.