Cornelius Bergen
2005-Dec-19 03:30 UTC
Is there a list of html_options for the FormOptionsHelper?
I''m looking for a way to select a default value from a select box, like this: select("user", "role_id", Role.find_all.collect {|r| [ r.name, r.id ] }, { :default_value => ''5'' } (of course, there is no ":default_value") If the form is being used on a ''create'' page, it should display a default in the select box. If the form is on the ''edit'' page, it should display the current value. Thanks, corny (2nd day on Rails, 1st post to the forum) -- Posted via http://www.ruby-forum.com/.
John Wilger
2005-Dec-19 03:46 UTC
Re: Is there a list of html_options for the FormOptionsHelper?
On 12/18/05, Cornelius Bergen <cornelius-4uiSEx/enRBBDgjK7y7TUQ@public.gmane.org> wrote:> I''m looking for a way to select a default value from a select box, like > this: > > select("user", "role_id", Role.find_all.collect {|r| [ r.name, r.id ] }, > { :default_value => ''5'' } > > (of course, there is no ":default_value") > > If the form is being used on a ''create'' page, it should display a > default in the select box. If the form is on the ''edit'' page, it should > display the current value.Assuming I''m understanding your question correctly, this should be handled automatically by the #select method. According to the API documentation [1]: The option currently held by the object will be selected, provided that the object is available. [1] http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000351 -- Regards, John Wilger http://johnwilger.com ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
Rick Olson
2005-Dec-19 03:49 UTC
Re: Is there a list of html_options for the FormOptionsHelper?
On 12/18/05, Cornelius Bergen <cornelius-4uiSEx/enRBBDgjK7y7TUQ@public.gmane.org> wrote:> I''m looking for a way to select a default value from a select box, like > this: > > select("user", "role_id", Role.find_all.collect {|r| [ r.name, r.id ] }, > { :default_value => ''5'' } > > (of course, there is no ":default_value") > > If the form is being used on a ''create'' page, it should display a > default in the select box. If the form is on the ''edit'' page, it should > display the current value.html_options are for attributes, like class. select is one of the form helpers that uses ActiveRecord. The default value is therefore whatever @user.role_id is. You should be able to set it in the controller. -- rick http://techno-weenie.net
Cornelius Bergen
2005-Dec-19 05:11 UTC
Re: Is there a list of html_options for the FormOptionsHelpe
Thanks Rick, that did it! John, if the object is NOT available I wanted to display a default option. In the ''Account/signup'' controller, I put: @user = User.new(@params[:user]) @roles = Role.find_all @user.role_id = ''5'' -- Posted via http://www.ruby-forum.com/.