Hiall, Unfortunately I just can''t find out how to setup a default selected value when using collection_select. My call is like so: <%= collection_select(:consultant, :lastname, @consultants, :id, :lastname, { :selected => @current_consultant.id } ) %> which is not working, I debugged so far that I know that @current_consultant.id contains the correct value. Any tips? cheers Martin
On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote:>Hiall, > >Unfortunately I just can''t find out how to setup a default selected >value when using collection_select. My call is like so: > ><%= collection_select(:consultant, :lastname, @consultants, :id, >:lastname, { :selected => @current_consultant.id } ) %> > >which is not working, I debugged so far that I know that >@current_consultant.id contains the correct value. > >Any tips? > >cheers >Martin > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails >What does the generated HTML look like? _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Hi Kevin, Sorry for replying late! The generated HTML looks like: <select id="communal_audit_consultant_lastname" name="communal_audit_consultant[lastname]"><option value="1">Auinger</option> <option value="2">Katterl</option></select> On 22 May 2006 18:22:50 -0000, Kevin Olbrich <devlists-rubyonrails@devlists.com> wrote:> > On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: > >Hiall, > > > >Unfortunately I just can''t find out how to setup a default selected > >value when using collection_select. My call is like so: > > > ><%= collection_select(:consultant, :lastname, @consultants, :id, > >:lastname, { :selected => @current_consultant.id } ) %> > > > >which is not working, I debugged so far that I know that > >@current_consultant.id contains the correct value. > > > >Any tips? > > > >cheers > >Martin > > > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > What does the generated HTML look like? > > _Kevin > > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote:>Hi Kevin, > >Sorry for replying late! The generated HTML looks like: > ><select id="communal_audit_consultant_lastname" >name="communal_audit_consultant[lastname]"><option >value="1">Auinger</option> ><option value="2">Katterl</option></select> > > > >On 22 May 2006 18:22:50 -0000, Kevin Olbrich ><devlists-rubyonrails@devlists.com> wrote: >> >> On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: >> >Hiall, >> > >> >Unfortunately I just can''t find out how to setup a default selected >> >value when using collection_select. My call is like so: >> > >> ><%= collection_select(:consultant, :lastname, @consultants, :id, >> >:lastname, { :selected => @current_consultant.id } ) %> >> > >> >which is not working, I debugged so far that I know that >> >@current_consultant.id contains the correct value. >> > >> >Any tips? >> > >> >cheers >> >Martin >> > >> >_______________________________________________ >> >Rails mailing list >> >Rails@lists.rubyonrails.org >> >http://lists.rubyonrails.org/mailman/listinfo/rails >> > >> What does the generated HTML look like? >> >> _Kevin >> >> -- >> Posted with http://DevLists.com. Sign up and save your mailbox. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails >try this... :selected => @current_consultant.id.to_s The ''values'' that the select compares against are really strings. Not sure if they get properly cast for the purposes of the comparison needed to set the selected one. _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
Thx for the tip but that didn''t work. In the meantime I found out that a call to <% options = options_from_collection_for_select(@consultants, :id, :lastname, @current_consultant.id) %> produces correct option tags like: <option value="1">Auinger</option> <option value="2" selected="selected">Katterl</option> However, I find no way to wrap this into a select tag. .. There must be an easier way??? cheers Martin On 22 May 2006 20:01:42 -0000, Kevin Olbrich <devlists-rubyonrails@devlists.com> wrote:> > On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote: > >Hi Kevin, > > > >Sorry for replying late! The generated HTML looks like: > > > ><select id="communal_audit_consultant_lastname" > >name="communal_audit_consultant[lastname]"><option > >value="1">Auinger</option> > ><option value="2">Katterl</option></select> > > > > > > > >On 22 May 2006 18:22:50 -0000, Kevin Olbrich > ><devlists-rubyonrails@devlists.com> wrote: > >> > >> On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: > >> >Hiall, > >> > > >> >Unfortunately I just can''t find out how to setup a default selected > >> >value when using collection_select. My call is like so: > >> > > >> ><%= collection_select(:consultant, :lastname, @consultants, :id, > >> >:lastname, { :selected => @current_consultant.id } ) %> > >> > > >> >which is not working, I debugged so far that I know that > >> >@current_consultant.id contains the correct value. > >> > > >> >Any tips? > >> > > >> >cheers > >> >Martin > >> > > >> >_______________________________________________ > >> >Rails mailing list > >> >Rails@lists.rubyonrails.org > >> >http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > >> What does the generated HTML look like? > >> > >> _Kevin > >> > >> -- > >> Posted with http://DevLists.com. Sign up and save your mailbox. > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > try this... > > :selected => @current_consultant.id.to_s > > The ''values'' that the select compares against are really strings. Not > sure if they get properly cast for the purposes of the comparison needed > to set the selected one. > > _Kevin > > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Monday, May 22, 2006, at 10:17 PM, Martin Gamsjaeger wrote:>Thx for the tip but that didn''t work. In the meantime I found out >that a call to ><% options = options_from_collection_for_select(@consultants, :id, >:lastname, @current_consultant.id) %> > >produces correct option tags like: > ><option value="1">Auinger</option> ><option value="2" selected="selected">Katterl</option> > >However, I find no way to wrap this into a select tag. .. > >There must be an easier way??? > >cheers >Martin > >On 22 May 2006 20:01:42 -0000, Kevin Olbrich ><devlists-rubyonrails@devlists.com> wrote: >> >> On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote: >> >Hi Kevin, >> > >> >Sorry for replying late! The generated HTML looks like: >> > >> ><select id="communal_audit_consultant_lastname" >> >name="communal_audit_consultant[lastname]"><option >> >value="1">Auinger</option> >> ><option value="2">Katterl</option></select> >> > >> > >> > >> >On 22 May 2006 18:22:50 -0000, Kevin Olbrich >> ><devlists-rubyonrails@devlists.com> wrote: >> >> >> >> On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: >> >> >Hiall, >> >> > >> >> >Unfortunately I just can''t find out how to setup a default selected >> >> >value when using collection_select. My call is like so: >> >> > >> >> ><%= collection_select(:consultant, :lastname, @consultants, :id, >> >> >:lastname, { :selected => @current_consultant.id } ) %> >> >> > >> >> >which is not working, I debugged so far that I know that >> >> >@current_consultant.id contains the correct value. >> >> > >> >> >Any tips? >> >> > >> >> >cheers >> >> >Martin >> >> > >> >> >_______________________________________________ >> >> >Rails mailing list >> >> >Rails@lists.rubyonrails.org >> >> >http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > >> >> What does the generated HTML look like? >> >> >> >> _Kevin >> >> >> >> -- >> >> Posted with http://DevLists.com. Sign up and save your mailbox. >> >> _______________________________________________ >> >> Rails mailing list >> >> Rails@lists.rubyonrails.org >> >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > >> >_______________________________________________ >> >Rails mailing list >> >Rails@lists.rubyonrails.org >> >http://lists.rubyonrails.org/mailman/listinfo/rails >> > >> >> try this... >> >> :selected => @current_consultant.id.to_s >> >> The ''values'' that the select compares against are really strings. Not >> sure if they get properly cast for the purposes of the comparison needed >> to set the selected one. >> >> _Kevin >> >> -- >> Posted with http://DevLists.com. Sign up and save your mailbox. >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails >Try <%= select_tag ''communal_audit_consultant_lastname'', options %> _Kevin -- Posted with http://DevLists.com. Sign up and save your mailbox.
I''ve tried that before :) This leads to very strange: <select id="communal_audit_consultant_lastname" name="communal_audit_consultant[lastname]"><option value="<option value="1">Auinger</option> "><option value="1">Auinger</option> </option> <option value="<option value="2" selected="selected">Katterl</option>"><option value="2" selected="selected">Katterl</option></option></select> showing the whole option strings in the dropdown ... On 22 May 2006 20:34:13 -0000, Kevin Olbrich <devlists-rubyonrails@devlists.com> wrote:> > On Monday, May 22, 2006, at 10:17 PM, Martin Gamsjaeger wrote: > >Thx for the tip but that didn''t work. In the meantime I found out > >that a call to > ><% options = options_from_collection_for_select(@consultants, :id, > >:lastname, @current_consultant.id) %> > > > >produces correct option tags like: > > > ><option value="1">Auinger</option> > ><option value="2" selected="selected">Katterl</option> > > > >However, I find no way to wrap this into a select tag. .. > > > >There must be an easier way??? > > > >cheers > >Martin > > > >On 22 May 2006 20:01:42 -0000, Kevin Olbrich > ><devlists-rubyonrails@devlists.com> wrote: > >> > >> On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote: > >> >Hi Kevin, > >> > > >> >Sorry for replying late! The generated HTML looks like: > >> > > >> ><select id="communal_audit_consultant_lastname" > >> >name="communal_audit_consultant[lastname]"><option > >> >value="1">Auinger</option> > >> ><option value="2">Katterl</option></select> > >> > > >> > > >> > > >> >On 22 May 2006 18:22:50 -0000, Kevin Olbrich > >> ><devlists-rubyonrails@devlists.com> wrote: > >> >> > >> >> On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: > >> >> >Hiall, > >> >> > > >> >> >Unfortunately I just can''t find out how to setup a default selected > >> >> >value when using collection_select. My call is like so: > >> >> > > >> >> ><%= collection_select(:consultant, :lastname, @consultants, :id, > >> >> >:lastname, { :selected => @current_consultant.id } ) %> > >> >> > > >> >> >which is not working, I debugged so far that I know that > >> >> >@current_consultant.id contains the correct value. > >> >> > > >> >> >Any tips? > >> >> > > >> >> >cheers > >> >> >Martin > >> >> > > >> >> >_______________________________________________ > >> >> >Rails mailing list > >> >> >Rails@lists.rubyonrails.org > >> >> >http://lists.rubyonrails.org/mailman/listinfo/rails > >> >> > > >> >> What does the generated HTML look like? > >> >> > >> >> _Kevin > >> >> > >> >> -- > >> >> Posted with http://DevLists.com. Sign up and save your mailbox. > >> >> _______________________________________________ > >> >> Rails mailing list > >> >> Rails@lists.rubyonrails.org > >> >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> >> > >> > > >> >_______________________________________________ > >> >Rails mailing list > >> >Rails@lists.rubyonrails.org > >> >http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > >> > >> try this... > >> > >> :selected => @current_consultant.id.to_s > >> > >> The ''values'' that the select compares against are really strings. Not > >> sure if they get properly cast for the purposes of the comparison needed > >> to set the selected one. > >> > >> _Kevin > >> > >> -- > >> Posted with http://DevLists.com. Sign up and save your mailbox. > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > Try > <%= select_tag ''communal_audit_consultant_lastname'', options %> > > _Kevin > > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I also tried to give the parameters as a hash with keys corresponding to parameters of options_from_collection_for_select. This also doesn''t lead to anything ... On 5/22/06, Martin Gamsjaeger <gamsnjaga@gmail.com> wrote:> I''ve tried that before :) This leads to very strange: > > <select id="communal_audit_consultant_lastname" > name="communal_audit_consultant[lastname]"><option value="<option > value="1">Auinger</option> > "><option value="1">Auinger</option> > </option> > <option value="<option value="2" > selected="selected">Katterl</option>"><option > value="2" > selected="selected">Katterl</option></option></select> > > showing the whole option strings in the dropdown ... > > > On 22 May 2006 20:34:13 -0000, Kevin Olbrich > <devlists-rubyonrails@devlists.com> wrote: > > > > On Monday, May 22, 2006, at 10:17 PM, Martin Gamsjaeger wrote: > > >Thx for the tip but that didn''t work. In the meantime I found out > > >that a call to > > ><% options = options_from_collection_for_select(@consultants, :id, > > >:lastname, @current_consultant.id) %> > > > > > >produces correct option tags like: > > > > > ><option value="1">Auinger</option> > > ><option value="2" selected="selected">Katterl</option> > > > > > >However, I find no way to wrap this into a select tag. .. > > > > > >There must be an easier way??? > > > > > >cheers > > >Martin > > > > > >On 22 May 2006 20:01:42 -0000, Kevin Olbrich > > ><devlists-rubyonrails@devlists.com> wrote: > > >> > > >> On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote: > > >> >Hi Kevin, > > >> > > > >> >Sorry for replying late! The generated HTML looks like: > > >> > > > >> ><select id="communal_audit_consultant_lastname" > > >> >name="communal_audit_consultant[lastname]"><option > > >> >value="1">Auinger</option> > > >> ><option value="2">Katterl</option></select> > > >> > > > >> > > > >> > > > >> >On 22 May 2006 18:22:50 -0000, Kevin Olbrich > > >> ><devlists-rubyonrails@devlists.com> wrote: > > >> >> > > >> >> On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: > > >> >> >Hiall, > > >> >> > > > >> >> >Unfortunately I just can''t find out how to setup a default selected > > >> >> >value when using collection_select. My call is like so: > > >> >> > > > >> >> ><%= collection_select(:consultant, :lastname, @consultants, :id, > > >> >> >:lastname, { :selected => @current_consultant.id } ) %> > > >> >> > > > >> >> >which is not working, I debugged so far that I know that > > >> >> >@current_consultant.id contains the correct value. > > >> >> > > > >> >> >Any tips? > > >> >> > > > >> >> >cheers > > >> >> >Martin > > >> >> > > > >> >> >_______________________________________________ > > >> >> >Rails mailing list > > >> >> >Rails@lists.rubyonrails.org > > >> >> >http://lists.rubyonrails.org/mailman/listinfo/rails > > >> >> > > > >> >> What does the generated HTML look like? > > >> >> > > >> >> _Kevin > > >> >> > > >> >> -- > > >> >> Posted with http://DevLists.com. Sign up and save your mailbox. > > >> >> _______________________________________________ > > >> >> Rails mailing list > > >> >> Rails@lists.rubyonrails.org > > >> >> http://lists.rubyonrails.org/mailman/listinfo/rails > > >> >> > > >> > > > >> >_______________________________________________ > > >> >Rails mailing list > > >> >Rails@lists.rubyonrails.org > > >> >http://lists.rubyonrails.org/mailman/listinfo/rails > > >> > > > >> > > >> try this... > > >> > > >> :selected => @current_consultant.id.to_s > > >> > > >> The ''values'' that the select compares against are really strings. Not > > >> sure if they get properly cast for the purposes of the comparison needed > > >> to set the selected one. > > >> > > >> _Kevin > > >> > > >> -- > > >> Posted with http://DevLists.com. Sign up and save your mailbox. > > >> _______________________________________________ > > >> Rails mailing list > > >> Rails@lists.rubyonrails.org > > >> http://lists.rubyonrails.org/mailman/listinfo/rails > > >> > > > > > >_______________________________________________ > > >Rails mailing list > > >Rails@lists.rubyonrails.org > > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > Try > > <%= select_tag ''communal_audit_consultant_lastname'', options %> > > > > _Kevin > > > > -- > > Posted with http://DevLists.com. Sign up and save your mailbox. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Going through the AWDWR book I decided to go another way. I now have @consultants = CommunalAuditConsultant.find(:all, :order => "lastname" ).map {|c| [c.lastname, c.id] } in my controller, and then <%= select(:communal_audit_consultant, :id, @consultants) %> in my partial This works perfectly! Thx for your patience and tips, Kevin! cheers Martin On 5/22/06, Martin Gamsjaeger <gamsnjaga@gmail.com> wrote:> I also tried to give the parameters as a hash with keys corresponding > to parameters of options_from_collection_for_select. This also doesn''t > lead to anything ... > > On 5/22/06, Martin Gamsjaeger <gamsnjaga@gmail.com> wrote: > > I''ve tried that before :) This leads to very strange: > > > > <select id="communal_audit_consultant_lastname" > > name="communal_audit_consultant[lastname]"><option value="<option > > value="1">Auinger</option> > > "><option value="1">Auinger</option> > > </option> > > <option value="<option value="2" > > selected="selected">Katterl</option>"><option > > value="2" > > selected="selected">Katterl</option></option></select> > > > > showing the whole option strings in the dropdown ... > > > > > > On 22 May 2006 20:34:13 -0000, Kevin Olbrich > > <devlists-rubyonrails@devlists.com> wrote: > > > > > > On Monday, May 22, 2006, at 10:17 PM, Martin Gamsjaeger wrote: > > > >Thx for the tip but that didn''t work. In the meantime I found out > > > >that a call to > > > ><% options = options_from_collection_for_select(@consultants, :id, > > > >:lastname, @current_consultant.id) %> > > > > > > > >produces correct option tags like: > > > > > > > ><option value="1">Auinger</option> > > > ><option value="2" selected="selected">Katterl</option> > > > > > > > >However, I find no way to wrap this into a select tag. .. > > > > > > > >There must be an easier way??? > > > > > > > >cheers > > > >Martin > > > > > > > >On 22 May 2006 20:01:42 -0000, Kevin Olbrich > > > ><devlists-rubyonrails@devlists.com> wrote: > > > >> > > > >> On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote: > > > >> >Hi Kevin, > > > >> > > > > >> >Sorry for replying late! The generated HTML looks like: > > > >> > > > > >> ><select id="communal_audit_consultant_lastname" > > > >> >name="communal_audit_consultant[lastname]"><option > > > >> >value="1">Auinger</option> > > > >> ><option value="2">Katterl</option></select> > > > >> > > > > >> > > > > >> > > > > >> >On 22 May 2006 18:22:50 -0000, Kevin Olbrich > > > >> ><devlists-rubyonrails@devlists.com> wrote: > > > >> >> > > > >> >> On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: > > > >> >> >Hiall, > > > >> >> > > > > >> >> >Unfortunately I just can''t find out how to setup a default selected > > > >> >> >value when using collection_select. My call is like so: > > > >> >> > > > > >> >> ><%= collection_select(:consultant, :lastname, @consultants, :id, > > > >> >> >:lastname, { :selected => @current_consultant.id } ) %> > > > >> >> > > > > >> >> >which is not working, I debugged so far that I know that > > > >> >> >@current_consultant.id contains the correct value. > > > >> >> > > > > >> >> >Any tips? > > > >> >> > > > > >> >> >cheers > > > >> >> >Martin > > > >> >> > > > > >> >> >_______________________________________________ > > > >> >> >Rails mailing list > > > >> >> >Rails@lists.rubyonrails.org > > > >> >> >http://lists.rubyonrails.org/mailman/listinfo/rails > > > >> >> > > > > >> >> What does the generated HTML look like? > > > >> >> > > > >> >> _Kevin > > > >> >> > > > >> >> -- > > > >> >> Posted with http://DevLists.com. Sign up and save your mailbox. > > > >> >> _______________________________________________ > > > >> >> Rails mailing list > > > >> >> Rails@lists.rubyonrails.org > > > >> >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > >> >> > > > >> > > > > >> >_______________________________________________ > > > >> >Rails mailing list > > > >> >Rails@lists.rubyonrails.org > > > >> >http://lists.rubyonrails.org/mailman/listinfo/rails > > > >> > > > > >> > > > >> try this... > > > >> > > > >> :selected => @current_consultant.id.to_s > > > >> > > > >> The ''values'' that the select compares against are really strings. Not > > > >> sure if they get properly cast for the purposes of the comparison needed > > > >> to set the selected one. > > > >> > > > >> _Kevin > > > >> > > > >> -- > > > >> Posted with http://DevLists.com. Sign up and save your mailbox. > > > >> _______________________________________________ > > > >> Rails mailing list > > > >> Rails@lists.rubyonrails.org > > > >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > >> > > > > > > > >_______________________________________________ > > > >Rails mailing list > > > >Rails@lists.rubyonrails.org > > > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > Try > > > <%= select_tag ''communal_audit_consultant_lastname'', options %> > > > > > > _Kevin > > > > > > -- > > > Posted with http://DevLists.com. Sign up and save your mailbox. > > > _______________________________________________ > > > Rails mailing list > > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > >
You could add in your controller method... @consultants.lastname = ''bla'' Craig On Mon, 2006-05-22 at 22:17 +0200, Martin Gamsjaeger wrote:> Thx for the tip but that didn''t work. In the meantime I found out that a call to > <% options = options_from_collection_for_select(@consultants, :id, > :lastname, @current_consultant.id) %> > > produces correct option tags like: > > <option value="1">Auinger</option> > <option value="2" selected="selected">Katterl</option> > > However, I find no way to wrap this into a select tag. .. > > There must be an easier way??? > > cheers > Martin > > On 22 May 2006 20:01:42 -0000, Kevin Olbrich > <devlists-rubyonrails@devlists.com> wrote: > > > > On Monday, May 22, 2006, at 9:35 PM, Martin Gamsjaeger wrote: > > >Hi Kevin, > > > > > >Sorry for replying late! The generated HTML looks like: > > > > > ><select id="communal_audit_consultant_lastname" > > >name="communal_audit_consultant[lastname]"><option > > >value="1">Auinger</option> > > ><option value="2">Katterl</option></select> > > > > > > > > > > > >On 22 May 2006 18:22:50 -0000, Kevin Olbrich > > ><devlists-rubyonrails@devlists.com> wrote: > > >> > > >> On Monday, May 22, 2006, at 7:56 PM, Martin Gamsjaeger wrote: > > >> >Hiall, > > >> > > > >> >Unfortunately I just can''t find out how to setup a default selected > > >> >value when using collection_select. My call is like so: > > >> > > > >> ><%= collection_select(:consultant, :lastname, @consultants, :id, > > >> >:lastname, { :selected => @current_consultant.id } ) %> > > >> > > > >> >which is not working, I debugged so far that I know that > > >> >@current_consultant.id contains the correct value. > > >> > > > >> >Any tips? > > >> > > > >> >cheers > > >> >Martin > > >> > > > >> >_______________________________________________ > > >> >Rails mailing list > > >> >Rails@lists.rubyonrails.org > > >> >http://lists.rubyonrails.org/mailman/listinfo/rails > > >> > > > >> What does the generated HTML look like? > > >> > > >> _Kevin > > >> > > >> -- > > >> Posted with http://DevLists.com. Sign up and save your mailbox. > > >> _______________________________________________ > > >> Rails mailing list > > >> Rails@lists.rubyonrails.org > > >> http://lists.rubyonrails.org/mailman/listinfo/rails > > >> > > > > > >_______________________________________________ > > >Rails mailing list > > >Rails@lists.rubyonrails.org > > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > try this... > > > > :selected => @current_consultant.id.to_s > > > > The ''values'' that the select compares against are really strings. Not > > sure if they get properly cast for the purposes of the comparison needed > > to set the selected one. > > > > _Kevin > > > > -- > > Posted with http://DevLists.com. Sign up and save your mailbox. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hey All: Martin Gamsjaeger wrote:> I''ve tried that before :) This leads to very strange: > > <select id="communal_audit_consultant_lastname" > name="communal_audit_consultant[lastname]"><option value="<option > value="1">Auinger</option> > "><option value="1">Auinger</option> > </option> > <option value="<option value="2" > selected="selected">Katterl</option>"><option > value="2" > selected="selected">Katterl</option></option></select> > > showing the whole option strings in the dropdown ... > > > On 22 May 2006 20:34:13 -0000, Kevin OlbrichThis has always worked for me - just follow the docs ;) <%= select_tag ''my[select]'', options_from_collection_for_select(Model.find(:all),''id'',''name'',some_string) %> where some_string = an actual string (i.e. ''Joe'') or a variable (ala @person.name). Works like a charm - everytime. Cheers, divotdave -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Martin is right, but would like to add a comment. If you do <%= select_tag ''my[select]'', options_from_collection_for_select(Model.find(:all),''id'',''name'',some_string) %> Then you do need to pass some_string as string. However, if you are passing symbols, the you would need to convert to the symbol''s type: <%= select_tag ''my[select]'', options_from_collection_for_select(Model.find(:all),:id,:name,some_string.to_i) %> Hope that helps someone Riclaso divotdave wrote:> Hey All: > > Martin Gamsjaeger wrote: >> I''ve tried that before :) This leads to very strange: >> >> <select id="communal_audit_consultant_lastname" >> name="communal_audit_consultant[lastname]"><option value="<option >> value="1">Auinger</option> >> "><option value="1">Auinger</option> >> </option> >> <option value="<option value="2" >> selected="selected">Katterl</option>"><option >> value="2" >> selected="selected">Katterl</option></option></select> >> >> showing the whole option strings in the dropdown ... >> >> >> On 22 May 2006 20:34:13 -0000, Kevin Olbrich > > This has always worked for me - just follow the docs ;) > > <%= select_tag ''my[select]'', > options_from_collection_for_select(Model.find(:all),''id'',''name'',some_string) > %> > > where some_string = an actual string (i.e. ''Joe'') or a variable (ala > @person.name). > > Works like a charm - everytime. > > Cheers, > divotdave-- Posted via http://www.ruby-forum.com/.
Hi, I have some issues with setting the selected value. On my page, user selects a State from the drop down and other attributes and click on "Find". I want to set the default value of the drop down to the value selected prior to clicking "Find". <%=collection_select(:state, :state_id, @states, :state_id, :state_name, {:prompt => true, :selected => @selected_state}, {:style => ''width:200px;'', :onchange => remote_function(:url => {:action => ''update_cities''},:with => "''state_id=''+value")})%> In my controller, if I have @selected_state = 1, the dropdown default value is shown in the UI. However, if I have @selected_state = state_id, the default value is not set. Snippet of the code in my controller: def find state_id = params[:state][:state_id] @selected_state = state_id /* Some logic here */ @states = State.find(:all) respond_to do |format| format.html { render :action => "index" } end end Any pointers are appreciated. Thanks. -- Posted via http://www.ruby-forum.com/.
This is useful about collection_select :) http://shiningthrough.co.uk/blog/show/6 On Sep 24, 12:13 am, Eunice Tan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > I have some issues with setting the selected value. On my page, user > selects a State from the drop down and other attributes and click on > "Find". I want to set the default value of the drop down to the value > selected prior to clicking "Find". > > <%=collection_select(:state, :state_id, @states, :state_id, :state_name, > {:prompt => true, :selected => @selected_state}, > {:style => ''width:200px;'', > :onchange => remote_function(:url => > {:action => ''update_cities''},:with => > "''state_id=''+value")})%> > > In my controller, if I have @selected_state = 1, the dropdown default > value is shown in the UI. However, if I have @selected_state > state_id, the default value is not set. > > Snippet of the code in my controller: > > def find > state_id = params[:state][:state_id] > > @selected_state = state_id > > /* Some logic here */ > > @states = State.find(:all) > > respond_to do |format| > format.html { render :action => "index" } > end > end > > Any pointers are appreciated. Thanks. > > -- > Posted viahttp://www.ruby-forum.com/.
Thanks for the link Zt Di. @selected_state = state_id.to_i works. This is because state_id = params[:state][:state_id] returns a string. -- Posted via http://www.ruby-forum.com/.
Agree with you . Someone should merge the documentation there to the Rails API. The rails api''s collection_select doc is somewhat incomplete or too brief. In short, it''s not helpful. On Sep 26, 9:08 pm, horliao <zetia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is useful aboutcollection_select:) > > http://shiningthrough.co.uk/blog/show/6 > > On Sep 24, 12:13 am, Eunice Tan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: