Hello all. I am having a bit of a mind blank and was wondering if anyone could give me a hand. I am planning on grouping users. These groups will be hardcoded somewhere. When I create a user I will assign them to a group based on a dropdown (which is currently totally HTML, including the options) which gets saved to the user profile. This works fine however when modifying the user the group isn''t being picked up of course because it is hardcoded HTML. So what I am trying to figure out is. Create a dropdown that lists all the hardcoded groups from an array in the code and defaults the dropdown to the one that is associated with the user profile. There aren''t a large number of groups and they aren''t going to change which is why I am not sticking them in a table. Cheers RJ -- 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 -~----------~----~----~----~------~----~------~--~---
RJ, Use the ActionView method <a href="http://api.rubyonrails.org/classes/ ActionView/Helpers/FormOptionsHelper.html#M000506">select</a>. For instance, lets say you have three groups: red, green, and blue and the column in your users table is "group". <%= select ''users'', ''group'', {"Red" => "red", "Green" => "green", "Blue" => "blue"}, {:include_blank => true} %> The keys (Red, Green, and Blue) are what are displayed in the actual drop down box. The values (red, green, and blue) are what are stored in the "group" column of the user''s row. By default the select method will automatically select whatever is in the user''s group column. For instance: @user = User.find(1) @user.group #=> "green" If you were to pull up a page with the above select tag for this user, the select tag should have the "Green" value automatically selected. Another note... if you have the possible group values hardcoded somewhere else in your code, you can replace the hardcoded options hash with a reference to the variables in your code (however, these must either be in a hash or an array format ... see docs for more details). So if you had: class User < ActiveRecord::Base GROUPS = { "Red" => "red", "Green" => "green", "Blue" => "blue" } end Then you could do: <%= select ''users'', ''group'', User.GROUPS, {:include_blank => true} %> On Mar 18, 10:32 pm, RJ <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello all. > > I am having a bit of a mind blank and was wondering if anyone could give > me a hand. > > I am planning on grouping users. These groups will be hardcoded > somewhere. When I create a user I will assign them to a group based on a > dropdown (which is currently totally HTML, including the options) which > gets saved to the user profile. > > This works fine however when modifying the user the group isn''t being > picked up of course because it is hardcoded HTML. > > So what I am trying to figure out is. Create a dropdown that lists all > the hardcoded groups from an array in the code and defaults the dropdown > to the one that is associated with the user profile. > > There aren''t a large number of groups and they aren''t going to change > which is why I am not sticking them in a table. > > Cheers > > RJ > > -- > 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 -~----------~----~----~----~------~----~------~--~---
This is great! class User < ActiveRecord::Base GROUPS = { "Red" => "red", "Green" => "green", "Blue" => "blue" } end Thanks. Ivor On 3/19/07, Marc Love <marcslove-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > RJ, > > Use the ActionView method <a href="http://api.rubyonrails.org/classes/ > ActionView/Helpers/FormOptionsHelper.html#M000506">select</a>. For > instance, lets say you have three groups: red, green, and blue and the > column in your users table is "group". > > <%= select ''users'', ''group'', {"Red" => "red", "Green" => "green", > "Blue" => "blue"}, {:include_blank => true} %> > > The keys (Red, Green, and Blue) are what are displayed in the actual > drop down box. The values (red, green, and blue) are what are stored > in the "group" column of the user''s row. By default the select method > will automatically select whatever is in the user''s group column. For > instance: > > @user = User.find(1) > @user.group #=> "green" > > If you were to pull up a page with the above select tag for this user, > the select tag should have the "Green" value automatically selected. > > Another note... if you have the possible group values hardcoded > somewhere else in your code, you can replace the hardcoded options > hash with a reference to the variables in your code (however, these > must either be in a hash or an array format ... see docs for more > details). So if you had: > > class User < ActiveRecord::Base > GROUPS = { > "Red" => "red", > "Green" => "green", > "Blue" => "blue" > } > end > > Then you could do: > > <%= select ''users'', ''group'', User.GROUPS, {:include_blank => true} %> > > On Mar 18, 10:32 pm, RJ <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > Hello all. > > > > I am having a bit of a mind blank and was wondering if anyone could give > > me a hand. > > > > I am planning on grouping users. These groups will be hardcoded > > somewhere. When I create a user I will assign them to a group based on a > > dropdown (which is currently totally HTML, including the options) which > > gets saved to the user profile. > > > > This works fine however when modifying the user the group isn''t being > > picked up of course because it is hardcoded HTML. > > > > So what I am trying to figure out is. Create a dropdown that lists all > > the hardcoded groups from an array in the code and defaults the dropdown > > to the one that is associated with the user profile. > > > > There aren''t a large number of groups and they aren''t going to change > > which is why I am not sticking them in a table. > > > > Cheers > > > > RJ > > > > -- > > 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 -~----------~----~----~----~------~----~------~--~---
Marc Love wrote:> RJ, > > Use the ActionView method <a href="http://api.rubyonrails.org/classes/ > ActionView/Helpers/FormOptionsHelper.html#M000506">select</a>. For > instance, lets say you have three groups: red, green, and blue and the > column in your users table is "group". > > <%= select ''users'', ''group'', {"Red" => "red", "Green" => "green", > "Blue" => "blue"}, {:include_blank => true} %> > > The keys (Red, Green, and Blue) are what are displayed in the actual > drop down box. The values (red, green, and blue) are what are stored > in the "group" column of the user''s row. By default the select method > will automatically select whatever is in the user''s group column. For > instance: > > @user = User.find(1) > @user.group #=> "green" > > If you were to pull up a page with the above select tag for this user, > the select tag should have the "Green" value automatically selected. > > Another note... if you have the possible group values hardcoded > somewhere else in your code, you can replace the hardcoded options > hash with a reference to the variables in your code (however, these > must either be in a hash or an array format ... see docs for more > details). So if you had: > > class User < ActiveRecord::Base > GROUPS = { > "Red" => "red", > "Green" => "green", > "Blue" => "blue" > } > end > > Then you could do: > > <%= select ''users'', ''group'', User.GROUPS, {:include_blank => true} %>Many thanks Marc! As you explained it I was in forehead slapping mode. I have a psychological problem dealing with selects and collections at the best of times so I think my brain involuntarily shutdown when confronted with a non-standard (to my experience) situation. Thank you very much for the detailed explanation. RJ -- 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 -~----------~----~----~----~------~----~------~--~---
No problem RJ, I''ve been there COUNTLESS times myself! :-) On Mar 19, 10:35 am, RJ <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Marc Love wrote: > > RJ, > > > Use the ActionView method <a href="http://api.rubyonrails.org/classes/ > > ActionView/Helpers/FormOptionsHelper.html#M000506">select</a>. For > > instance, lets say you have three groups: red, green, and blue and the > > column in your users table is "group". > > > <%= select ''users'', ''group'', {"Red" => "red", "Green" => "green", > > "Blue" => "blue"}, {:include_blank => true} %> > > > The keys (Red, Green, and Blue) are what are displayed in the actual > > drop down box. The values (red, green, and blue) are what are stored > > in the "group" column of the user''s row. By default the select method > > will automatically select whatever is in the user''s group column. For > > instance: > > > @user = User.find(1) > > @user.group #=> "green" > > > If you were to pull up a page with the above select tag for this user, > > the select tag should have the "Green" value automatically selected. > > > Another note... if you have the possible group values hardcoded > > somewhere else in your code, you can replace the hardcoded options > > hash with a reference to the variables in your code (however, these > > must either be in a hash or an array format ... see docs for more > > details). So if you had: > > > class User < ActiveRecord::Base > > GROUPS = { > > "Red" => "red", > > "Green" => "green", > > "Blue" => "blue" > > } > > end > > > Then you could do: > > > <%= select ''users'', ''group'', User.GROUPS, {:include_blank => true} %> > > Many thanks Marc! As you explained it I was in forehead slapping mode. I > have a psychological problem dealing with selects and collections at the > best of times so I think my brain involuntarily shutdown when confronted > with a non-standard (to my experience) situation. > > Thank you very much for the detailed explanation. > > RJ > > -- > 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 -~----------~----~----~----~------~----~------~--~---