How do I create a radio_button_tag for every value of a constant array? E.g., I have this constant array: VALID_ORIENTATIONS ["Straight", "Gay", "Bisexual"] . A user is presented with a form to search for other users according to sexual orientation. Currently, the form looks like this: <div class="form_row"> <label for="orientation">Sexual Orientation:</label> <%= radio_button_tag :orientation, "Straight", params[:orientation] == ''Straight'', :id => "Straight" %>Straight <%= radio_button_tag :orientation, "Gay", params[:orientation] == ''Gay'', :id => "Gay" %>Gay <%= radio_button_tag :orientation, "Bisexual", params[:orientation] == ''Bisexual'', :id => "Bisexual" %>Bisexual <%= radio_button_tag :orientation, "", params[:orientation].blank?, :id => "Any" %>Any </div> This is not DRY. How can I iterate through every value in the constant array to make these radio buttons? Is partial appropriate for this? The array has only three values. Also: the blank in the last radio button is obviously not in the array. It''s there to allow the user to not use this search criterion. Is there a better way for a user to unselect a radio button without choosing any other? Thanks. -- 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.
Marnen Laibow-Koser
2010-Feb-06 05:45 UTC
Re: radio_button_tag for every value of constant array
Learn by Doing wrote:> How do I create a radio_button_tag for every value of a constant > array? E.g., I have this constant array: VALID_ORIENTATIONS > ["Straight", "Gay", "Bisexual"] . A user is presented with a form to > search for other users according to sexual orientation. Currently, > the form looks like this: > > <div class="form_row"> > <label for="orientation">Sexual Orientation:</label> > <%= radio_button_tag :orientation, "Straight", > params[:orientation] == ''Straight'', > :id => "Straight" %>Straight > <%= radio_button_tag :orientation, "Gay", > params[:orientation] == ''Gay'', > :id => "Gay" %>Gay > <%= radio_button_tag :orientation, "Bisexual", > params[:orientation] == ''Bisexual'', > :id => "Bisexual" %>Bisexual > <%= radio_button_tag :orientation, "", > params[:orientation].blank?, > :id => "Any" %>Any > </div> > > This is not DRY. How can I iterate through every value in the > constant array to make these radio buttons? Is partial appropriate > for this? The array has only three values. >You used the word "iterate" to describe what you want to do. Does that ring any bells? Right! Use Array#each or render :partial, :collection. Also: consider using the array indices, not the actual words, for the value attributes of the buttons. BTW, why aren''t you using Haml?> Also: the blank in the last radio button is obviously not in the > array. It''s there to allow the user to not use this search > criterion. Is there a better way for a user to unselect a radio > button without choosing any other?No. Use a <select> element instead (and check out collection_select).> > Thanks.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Marnen. Using the array index and partial is a great idea. The select and collection_select are also promising. I''ll take a look at Haml. Thanks. On Feb 5, 9:45 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Learn by Doing wrote: > > How do I create a radio_button_tag for every value of a constant > > array? E.g., I have this constant array: VALID_ORIENTATIONS > > ["Straight", "Gay", "Bisexual"] . A user is presented with a form to > > search for other users according to sexual orientation. Currently, > > the form looks like this: > > > <div class="form_row"> > > <label for="orientation">Sexual Orientation:</label> > > <%= radio_button_tag :orientation, "Straight", > > params[:orientation] == ''Straight'', > > :id => "Straight" %>Straight > > <%= radio_button_tag :orientation, "Gay", > > params[:orientation] == ''Gay'', > > :id => "Gay" %>Gay > > <%= radio_button_tag :orientation, "Bisexual", > > params[:orientation] == ''Bisexual'', > > :id => "Bisexual" %>Bisexual > > <%= radio_button_tag :orientation, "", > > params[:orientation].blank?, > > :id => "Any" %>Any > > </div> > > > This is not DRY. How can I iterate through every value in the > > constant array to make these radio buttons? Is partial appropriate > > for this? The array has only three values. > > You used the word "iterate" to describe what you want to do. Does that > ring any bells? Right! Use Array#each or render :partial, :collection. > > Also: consider using the array indices, not the actual words, for the > value attributes of the buttons. > > BTW, why aren''t you using Haml? > > > Also: the blank in the last radio button is obviously not in the > > array. It''s there to allow the user to not use this search > > criterion. Is there a better way for a user to unselect a radio > > button without choosing any other? > > No. Use a <select> element instead (and check out collection_select). > > > > > Thanks. > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Marnen, The select and collection_select require an instance variable to be passed to the view. But I am using form_tag and not form_for. So I don''t have an object to work with. This is a search form, the user does not need to log in. So does it make sense to switch to "form_for" for this search form in order to have an object to work with? Thanks. On Feb 6, 3:01 pm, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Marnen. Using the array index and partial is a great idea. > The select and collection_select are also promising. > > I''ll take a look at Haml. > > Thanks. > > On Feb 5, 9:45 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > > Learn by Doing wrote: > > > How do I create a radio_button_tag for every value of a constant > > > array? E.g., I have this constant array: VALID_ORIENTATIONS > > > ["Straight", "Gay", "Bisexual"] . A user is presented with a form to > > > search for other users according to sexual orientation. Currently, > > > the form looks like this: > > > > <div class="form_row"> > > > <label for="orientation">Sexual Orientation:</label> > > > <%= radio_button_tag :orientation, "Straight", > > > params[:orientation] == ''Straight'', > > > :id => "Straight" %>Straight > > > <%= radio_button_tag :orientation, "Gay", > > > params[:orientation] == ''Gay'', > > > :id => "Gay" %>Gay > > > <%= radio_button_tag :orientation, "Bisexual", > > > params[:orientation] == ''Bisexual'', > > > :id => "Bisexual" %>Bisexual > > > <%= radio_button_tag :orientation, "", > > > params[:orientation].blank?, > > > :id => "Any" %>Any > > > </div> > > > > This is not DRY. How can I iterate through every value in the > > > constant array to make these radio buttons? Is partial appropriate > > > for this? The array has only three values. > > > You used the word "iterate" to describe what you want to do. Does that > > ring any bells? Right! Use Array#each or render :partial, :collection. > > > Also: consider using the array indices, not the actual words, for the > > value attributes of the buttons. > > > BTW, why aren''t you using Haml? > > > > Also: the blank in the last radio button is obviously not in the > > > array. It''s there to allow the user to not use this search > > > criterion. Is there a better way for a user to unselect a radio > > > button without choosing any other? > > > No. Use a <select> element instead (and check out collection_select). > > > > Thanks. > > > Best, > > -- > > Marnen Laibow-Koserhttp://www.marnen.org > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Feb-07 04:38 UTC
Re: radio_button_tag for every value of constant array
Learn by Doing wrote:> Hi Marnen, > > The select and collection_select require an instance variable to be > passed to the view. But I am using form_tag and not form_for. So I > don''t have an object to work with.So use select_tag.> This is a search form, the user > does not need to log in. So does it make sense to switch to > "form_for" for this search form in order to have an object to work > with?Usually, yes. Do you have a Search class?> > Thanks.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Marnen, Thank you so much for pointing me to using a Search class. I watched the screencast on this http://media.railscasts.com/videos/111_advanced_search_form.mov and realized this is what I should do so that the app can remember a user''s search. My question now is How do I store the value of multiple checkboxes as a result of using "check_box" helper in a "Search" class? Please see my thread on this question here: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/50915dfb233d62bc# BTW, yes I should have looked at select_tag more closely. Thanks! On Feb 6, 8:38 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Learn by Doing wrote: > > Hi Marnen, > > > The select and collection_select require an instance variable to be > > passed to the view. But I am using form_tag and not form_for. So I > > don''t have an object to work with. > > So use select_tag. > > > This is a search form, the user > > does not need to log in. So does it make sense to switch to > > "form_for" for this search form in order to have an object to work > > with? > > Usually, yes. Do you have a Search class? > > > > > Thanks. > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.