Peter Laurens
2007-May-31 15:08 UTC
Newbie: Modifying blank option from ''collection_select''
Hi all, Very new to RoR, but having great fun so far. I have a question which I thought would be a common one but haven''t found a clear answer on the forums through searching, so apologies: I want to have a select combo-box on a page, and am using "collection_select" to create it. I wanted to give the user a blank option to choose from, and so found ":include_blank => true", which works perfectly. However: The blank entry does not have any text assigned to the option, and so could be a little vague for the user, I would like to give this blank option text like "(none)" or something similar. I am beginning to believe I can''t use collection_select to do this, but need to use "select", is this the case? Perhaps someone would be kind enough to point out the differences, and get me started. Many thanks for helping out, it''s much appeciated, and again, apologies if this is a dumb question, - Peter -- 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 -~----------~----~----~----~------~----~------~--~---
Bill Kocik
2007-May-31 17:03 UTC
Re: Newbie: Modifying blank option from ''collection_select''
Hi Peter - On May 31, 11:08 am, Peter Laurens <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I want to have a select combo-box on a page, and am using > "collection_select" to create it. I wanted to give the user a blank > option to choose from, and so found ":include_blank => true", which > works perfectly. > > However: The blank entry does not have any text assigned to the option, > and so could be a little vague for the user, I would like to give this > blank option text like "(none)" or something similar. I am beginning to > believe I can''t use collection_select to do this, but need to use > "select", is this the case? Perhaps someone would be kind enough to > point out the differences, and get me started. > > Many thanks for helping out, it''s much appeciated, and again, apologies > if this is a dumb questionNot a dumb question at all. I''m doing pretty much what you''re doing in my own app, and I did it this way: <%= collection_select(:security_question, :id, SecurityQuestion.find(:all), :id, :question, options ={:prompt => "(Select a question)"}, :class =>"security_question") %> The trick is in this: options ={:prompt => "(Select a question)"} That makes the first line of HTML spit out for the select look like this: <select class="security_question" id="security_question_id" name="security_question[id]"><option value="">(Select a question)</ option> You can see that when the user loads the form, that select says "(Select a question)", and that option has no value - so if it''s left that way when they submit the form, there''ll be no value associated with the ID of the select passed to your controller, so you can tell they didn''t choose anything. Does that get you where you want to go? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Laurens
2007-May-31 17:15 UTC
Re: Newbie: Modifying blank option from ''collection_select''
Hi Bill, thanks for your advice, ":prompt" does indeed give the the ability to name the ''nil value'' as it were, which is exactly what was after, thanks. But unfortunately, when an object does have something other than nil for the field represented by the drop-down, the prompt disappears (e.g. on edit screens etc.) - the result of this appears to be that once a user has chosen an item from the list, they cannot ''un-choose'' and go back to nil for that field again :( ":include_blank" always rendered in the drop-down, and so allowed a user to undo their selection in an edit at a later time. ":prompt" seems to turn this into a one-way process :( Have I missed something? Many thanks, - Peter -- 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 -~----------~----~----~----~------~----~------~--~---
Bill Kocik
2007-May-31 17:56 UTC
Re: Newbie: Modifying blank option from ''collection_select''
Odd. You''ve just pointed out to me that I have a bug in my own code, though it''s not the same as what you''re seeing. Instead, what I see when I go to an edit form is that rather than what the user had selected from that drop-down being the chosen option, the default "(Select a question)" option is not only present, but selected. I''ll have to fix that, but I don''t know how yet. If I figure this all out I''ll post back and let you know how I did it. On May 31, 1:15 pm, Peter Laurens <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi Bill, > > thanks for your advice, ":prompt" does indeed give the the ability to > name the ''nil value'' as it were, which is exactly what was after, > thanks. But unfortunately, when an object does have something other than > nil for the field represented by the drop-down, the prompt disappears > (e.g. on edit screens etc.) - the result of this appears to be that once > a user has chosen an item from the list, they cannot ''un-choose'' and go > back to nil for that field again :( > > ":include_blank" always rendered in the drop-down, and so allowed a user > to undo their selection in an edit at a later time. ":prompt" seems to > turn this into a one-way process :( > > Have I missed something? > > Many thanks, > > - Peter > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Roger Pack
2007-Sep-28 16:44 UTC
Re: Newbie: Modifying blank option from ''collection_select''
Anybody ever figure out know how to modify (well) the ''name'' given the blank option, should you use :include_blank => true for a drop down? Thanks all! -Roger Peter Laurens wrote:> Hi Bill, > > thanks for your advice, ":prompt" does indeed give the the ability to > name the ''nil value'' as it were, which is exactly what was after, > thanks. But unfortunately, when an object does have something other than > nil for the field represented by the drop-down, the prompt disappears > (e.g. on edit screens etc.) - the result of this appears to be that once > a user has chosen an item from the list, they cannot ''un-choose'' and go > back to nil for that field again :( > > ":include_blank" always rendered in the drop-down, and so allowed a user > to undo their selection in an edit at a later time. ":prompt" seems to > turn this into a one-way process :( > > Have I missed something? > > Many thanks, > > - Peter-- 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 -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Sep-28 17:01 UTC
Re: Newbie: Modifying blank option from ''collection_select''
For something custom like this, you may just have to roll your own using a helper like this: #pass in the name instead of true like, :include_blank => ''Leave Blank'' def collection_select_named_blank(object, method, collection, value_method, text_method, options = {}, html_options = {}) sel_options = options_from_collection_for_select(collection,value_method,text_method,object.send(value_method)) sel_options += ''<option value="">''+options[:include_blank]+''</option>'' if options.has_key? :include_blank select_tag "#{object}[#{method}]", sel_options, html_options end There is probably a better way to do this, but this is what I came up with off the top of my head. I didn''t test the above code, so you will need to. Hopefully this will send you in the correct direction. Roger Pack wrote:> Anybody ever figure out know how to modify (well) the ''name'' given the > blank option, should you use :include_blank => true for a drop down? > Thanks all! > -Roger > Peter Laurens wrote: > >> Hi Bill, >> >> thanks for your advice, ":prompt" does indeed give the the ability to >> name the ''nil value'' as it were, which is exactly what was after, >> thanks. But unfortunately, when an object does have something other than >> nil for the field represented by the drop-down, the prompt disappears >> (e.g. on edit screens etc.) - the result of this appears to be that once >> a user has chosen an item from the list, they cannot ''un-choose'' and go >> back to nil for that field again :( >> >> ":include_blank" always rendered in the drop-down, and so allowed a user >> to undo their selection in an edit at a later time. ":prompt" seems to >> turn this into a one-way process :( >> >> Have I missed something? >> >> Many thanks, >> >> - Peter >> > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Sep-28 17:07 UTC
Re: Newbie: Modifying blank option from ''collection_select''
oops on the naming of the select tag #pass in the name instead of true like, :include_blank => ''Leave Blank'' def collection_select_named_blank(object, method, collection, value_method, text_method, options = {}, html_options = {}) sel_options = options_from_collection_for_select(collection,value_method,text_method,object.send(value_method)) sel_options += ''<option value="">''+options[:include_blank]+''</option>'' if options.has_key? :include_blank select_tag "#{object.class.humanize}[#{method}]", sel_options, html_options end William Pratt wrote:> For something custom like this, you may just have to roll your own > using a helper like this: > > #pass in the name instead of true like, :include_blank => ''Leave Blank'' > > def collection_select_named_blank(object, method, collection, > value_method, text_method, options = {}, html_options = {}) > sel_options = > options_from_collection_for_select(collection,value_method,text_method,object.send(value_method)) > sel_options += ''<option > value="">''+options[:include_blank]+''</option>'' if options.has_key? > :include_blank > select_tag "#{object.class.humanize}[#{method}]", sel_options, > html_options > end > > There is probably a better way to do this, but this is what I came up > with off the top of my head. I didn''t test the above code, so you will > need to. Hopefully this will send you in the correct direction. > > Roger Pack wrote: >> Anybody ever figure out know how to modify (well) the ''name'' given the >> blank option, should you use :include_blank => true for a drop down? >> Thanks all! >> -Roger >> Peter Laurens wrote: >> >>> Hi Bill, >>> >>> thanks for your advice, ":prompt" does indeed give the the ability to >>> name the ''nil value'' as it were, which is exactly what was after, >>> thanks. But unfortunately, when an object does have something other than >>> nil for the field represented by the drop-down, the prompt disappears >>> (e.g. on edit screens etc.) - the result of this appears to be that once >>> a user has chosen an item from the list, they cannot ''un-choose'' and go >>> back to nil for that field again :( >>> >>> ":include_blank" always rendered in the drop-down, and so allowed a user >>> to undo their selection in an edit at a later time. ":prompt" seems to >>> turn this into a one-way process :( >>> >>> Have I missed something? >>> >>> Many thanks, >>> >>> - Peter >>> >> >> > > -- > Sincerely, > > William Pratt > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Niaz Arifin
2007-Oct-08 18:00 UTC
Re: Newbie: Modifying blank option from ''collection_select''
Hi Peter, Thought you might cast some light on these: 1. How can I set a default value in a collection_select? To be specific, I have a class ''Platform'', and I want to set the platform_code (a field of Platform) to be ''ORA'' (which exists in the Platform table). I tried many variations of the following code in my view, but in vain: <% @selected = ''ORA'' %> <%= collection_select "sac_table_platform", "platform_code", Platform.find(:all, :order => "platform_code"), :platform_code, :platform_code, { :selected => @selected } %> 2. How can I build my own list of collection_select, e.g., a list of three people, say, to show [Adam, Bob, Charley] on the collection_select for the corresponding values [''A'', ''B'', ''C''] in a table (which already exists), and use it in my view? Thanks, Arif Peter Laurens wrote:> Hi all, > > Very new to RoR, but having great fun so far. I have a question which I > thought would be a common one but haven''t found a clear answer on the > forums through searching, so apologies: > > I want to have a select combo-box on a page, and am using > "collection_select" to create it. I wanted to give the user a blank > option to choose from, and so found ":include_blank => true", which > works perfectly. > > However: The blank entry does not have any text assigned to the option, > and so could be a little vague for the user, I would like to give this > blank option text like "(none)" or something similar. I am beginning to > believe I can''t use collection_select to do this, but need to use > "select", is this the case? Perhaps someone would be kind enough to > point out the differences, and get me started. > > Many thanks for helping out, it''s much appeciated, and again, apologies > if this is a dumb question, > > - Peter-- 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 -~----------~----~----~----~------~----~------~--~---
Roger Pack
2007-Nov-13 18:44 UTC
Re: Newbie: Modifying blank option from ''collection_select''
William Pratt wrote:> > William Pratt wrote: >> value="">''+options[:include_blank]+''</option>'' if options.has_key? >>> Anybody ever figure out know how to modify (well) the ''name'' given the >>>> nil for the field represented by the drop-down, the prompt disappears >>>> Many thanks,Thanks William! Also I think :prompt => ''whatever'' might work for certain tasks. Take care! -Roger -- 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 -~----------~----~----~----~------~----~------~--~---
Roger Roger
2010-Oct-07 21:02 UTC
Re: Newbie: Modifying blank option from ''collection_select''
I found this thread googling today. I see it''s older, and there may be other solutions by now, but I thought I''d use the opportunity to document something that worked for me: <%= form_builder_object.collection_select :my_model_id, MyModel.all.insert(0, MyModel.new(:name => "None") ), :id, :name %> The trick is to make a new instance (which doesn''t have an id yet), and then insert that at the beginning of the collection. This makes the text "None" show up first, thus be the default selection. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.