Steve Davie
2009-Feb-06 16:35 UTC
collection_select :selected (to get a default value into <se
I''m getting murdered by this thing! For the life of me, I can''t figure it out. I''ve spent literally days on google. Here''s what I would like to do (in rails 2.2.2): <% if params[:cruise_info] %> <%= collection_select :cabin_type, :cabin_type_id, CabinType.find_by_status(1).collect {|ct| ct}, :id, :name, {:selected => params[:cruise_info][:cabin_type_id]}%> <% end %> The intent here is, on re-rendering the page (after a validation failure), I don''t want the end-user to be required to make the selection again. I have half a dozen collection_selects in this form that are being returned from partials and are embeded with :fields_for, so they''re not automatically retaining their selections. Most of my search results are Bug reports for this, but they''re more than 2 years old. It''s gotta be fixed by now right? I have to be doing something wrong? I just can''t see it. I''ve tried using select, options_from_collection_for_select, and have the same issue with all of them. I''ve tried different variations, like putting it in the options => {} and in html_options => {}...and nothing. I''ve tried :selected, :selected_value, I even tried using :include_blank => {...}, which i knew wouldn''t work, but thought it worth a try. I''ve tried to_s on the params (which btw are an integer), but i''m not even seeing the "selected" option showing up in the html source. Sorry for being so long winded, I want to insure I include enough information to get this solved. Any advice? Thanks in advance -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-06 16:47 UTC
Re: collection_select :selected (to get a default value into <se
On 6 Feb 2009, at 16:35, Steve Davie wrote:> > I''m getting murdered by this thing! For the life of me, I can''t > figure > it out. I''ve spent literally days on google. > > Here''s what I would like to do (in rails 2.2.2): > > > <% if params[:cruise_info] %> > <%= collection_select :cabin_type, :cabin_type_id, > CabinType.find_by_status(1).collect {|ct| ct}, :id, :name, > {:selected => > params[:cruise_info][:cabin_type_id]}%> > <% end %>collection_select ignore the :selected option. Fred> > > The intent here is, on re-rendering the page (after a validation > failure), I don''t want the end-user to be required to make the > selection > again. I have half a dozen collection_selects in this form that are > being returned from partials and are embeded with :fields_for, so > they''re not automatically retaining their selections. > > Most of my search results are Bug reports for this, but they''re more > than 2 years old. It''s gotta be fixed by now right? I have to be > doing > something wrong? I just can''t see it. I''ve tried using select, > options_from_collection_for_select, and have the same issue with all > of > them. I''ve tried different variations, like putting it in the options > => {} and in html_options => {}...and nothing. I''ve tried :selected, > :selected_value, I even tried using :include_blank => {...}, which i > knew wouldn''t work, but thought it worth a try. I''ve tried to_s on > the > params (which btw are an integer), but i''m not even seeing the > "selected" option showing up in the html source. > > Sorry for being so long winded, I want to insure I include enough > information to get this solved. > > Any advice? > > Thanks in advance > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-06 16:48 UTC
Re: collection_select :selected (to get a default value into <se
On 6 Feb 2009, at 16:47, Frederick Cheung wrote:> > On 6 Feb 2009, at 16:35, Steve Davie wrote: > >> >> I''m getting murdered by this thing! For the life of me, I can''t >> figure >> it out. I''ve spent literally days on google. >> >> Here''s what I would like to do (in rails 2.2.2): >> >> >> <% if params[:cruise_info] %> >> <%= collection_select :cabin_type, :cabin_type_id, >> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name, >> {:selected => >> params[:cruise_info][:cabin_type_id]}%> >> <% end %> > > collection_select ignore the :selected option. >should add that this will change in 2.3 Fred> Fred > >> >> >> The intent here is, on re-rendering the page (after a validation >> failure), I don''t want the end-user to be required to make the >> selection >> again. I have half a dozen collection_selects in this form that are >> being returned from partials and are embeded with :fields_for, so >> they''re not automatically retaining their selections. >> >> Most of my search results are Bug reports for this, but they''re more >> than 2 years old. It''s gotta be fixed by now right? I have to be >> doing >> something wrong? I just can''t see it. I''ve tried using select, >> options_from_collection_for_select, and have the same issue with >> all of >> them. I''ve tried different variations, like putting it in the >> options >> => {} and in html_options => {}...and nothing. I''ve tried :selected, >> :selected_value, I even tried using :include_blank => {...}, which i >> knew wouldn''t work, but thought it worth a try. I''ve tried to_s on >> the >> params (which btw are an integer), but i''m not even seeing the >> "selected" option showing up in the html source. >> >> Sorry for being so long winded, I want to insure I include enough >> information to get this solved. >> >> Any advice? >> >> Thanks in advance >> -- >> 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 -~----------~----~----~----~------~----~------~--~---
Steve Davie
2009-Feb-06 16:55 UTC
Re: collection_select :selected (to get a default value into
Frederick Cheung wrote:> On 6 Feb 2009, at 16:47, Frederick Cheung wrote: > >>> >>> <% if params[:cruise_info] %> >>> <%= collection_select :cabin_type, :cabin_type_id, >>> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name, >>> {:selected => >>> params[:cruise_info][:cabin_type_id]}%> >>> <% end %> >> >> collection_select ignore the :selected option. >> > should add that this will change in 2.3 > > FredThanks for the info Fred, but do you know of any convention for handling this kind of problem now? -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Feb-06 17:50 UTC
Re: collection_select :selected (to get a default value into
On 6 Feb 2009, at 16:55, Steve Davie wrote:> > Frederick Cheung wrote: >> On 6 Feb 2009, at 16:47, Frederick Cheung wrote: >> >>>> >>>> <% if params[:cruise_info] %> >>>> <%= collection_select :cabin_type, :cabin_type_id, >>>> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name, >>>> {:selected => >>>> params[:cruise_info][:cabin_type_id]}%> >>>> <% end %> >>> >>> collection_select ignore the :selected option. >>> >> should add that this will change in 2.3 > > Thanks for the info Fred, but do you know of any convention for > handling > this kind of problem now?You could just use a regular select. Or if the cabin_type_id attribute of the corresponding model object is set to params[:cruise_info] [:cabin_type_id] then it will just work. Fred> > -- > 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 -~----------~----~----~----~------~----~------~--~---
Steve Davie
2009-Feb-06 18:40 UTC
Re: collection_select :selected (to get a default value into
Frederick Cheung wrote:> On 6 Feb 2009, at 16:55, Steve Davie wrote: > >>>>> <% end %> >>>> >>>> collection_select ignore the :selected option. >>>> >>> should add that this will change in 2.3 >> >> Thanks for the info Fred, but do you know of any convention for >> handling >> this kind of problem now? > > You could just use a regular select. Or if the cabin_type_id attribute > of the corresponding model object is set to params[:cruise_info] > [:cabin_type_id] then it will just work. > > FredThanks again Fred, I think I''ll have to just write out the selects. I was hoping that maybe this was more common and there would be a "rails" solution for it. The engineering of my forms must be out of whack with convention. I''ll swing over to railscasts for a re-review of complex forms. -- 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 -~----------~----~----~----~------~----~------~--~---
Julian Leviston
2009-Feb-07 03:51 UTC
Re: collection_select :selected (to get a default value into
I''m a little unsure as to what you want to do? Is it that you want a default value and then to retain their selected value? Or something else. I remember understanding collection select took me a while. It helps if you can do the HTML long hand once through to get clear about exactly what behaviour you want at first. Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 07/02/2009, at 3:55 AM, Steve Davie <rails-mailing-list@andreas- s.net> wrote:> > Frederick Cheung wrote: >> On 6 Feb 2009, at 16:47, Frederick Cheung wrote: >> >>>> >>>> <% if params[:cruise_info] %> >>>> <%= collection_select :cabin_type, :cabin_type_id, >>>> CabinType.find_by_status(1).collect {|ct| ct}, :id, :name, >>>> {:selected => >>>> params[:cruise_info][:cabin_type_id]}%> >>>> <% end %> >>> >>> collection_select ignore the :selected option. >>> >> should add that this will change in 2.3 >> >> Fred > > Thanks for the info Fred, but do you know of any convention for > handling > this kind of problem now? > -- > 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 -~----------~----~----~----~------~----~------~--~---
Miguel Regedor
2009-Feb-07 12:28 UTC
Re: collection_select :selected (to get a default value into <se
if you have a form you can do: f.collection_select :cabin_type_id, CabinType.find_by_status (1).collect {|ct| ct}, :id, :name, :prompt => " -- Select -- " and it should mantain the selected values. Other way (because collection_select ignore the :selected option) I would sugest you to use a select_tag in conjunction with options_for select. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I suggest that you buy and watch Forms screencasts by Ryan Bates published by Pragmatic Programmers. He explains proper use of collection_select. I have learned a lot by watching the screencasts. http://www.pragprog.com/screencasts/v-rbforms/mastering-rails-forms As per answering your question, I am also not sure of what is it that you are trying to do? If you are simply trying to set initial/ selected default value when creating/editing a record, then you should be doing that in the controller action and not in the view. Ryan explains it very clearly in his screencasts. Bharat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---