sfdesigner
2006-Jul-08 22:25 UTC
[Rails] Trying to create pulldown select menu for US State form fiel
I noticed there is a country_select. Is there anything like us_state_select maybe? More specifically - I''m wondering if I manually have to populate an array with all the U.S. state abbreviations so users can select which state they live in while entering their mailing address. -- Posted via http://www.ruby-forum.com/.
Craig White
2006-Jul-09 01:36 UTC
[Rails] Trying to create pulldown select menu for US State form fiel
On Sun, 2006-07-09 at 00:24 +0200, sfdesigner wrote:> I noticed there is a country_select. Is there anything like > us_state_select maybe? > > More specifically - I''m wondering if I manually have to populate an > array with all the U.S. state abbreviations so users can select which > state they live in while entering their mailing address.---- I would recommend that route...yes Craig
sfdesigner
2006-Jul-09 08:44 UTC
[Rails] Re: Trying to create pulldown select menu for US State form
Craig White wrote:> I would recommend that route...yes >I think I''ve got it now. Thanks for the pointer. For new and edit I have a partial _form.rhtml that is includes this... <% us_states = [["","Select State..."], ["AL","Alabama"], ["AK","Alaska"], ["AZ","Arizona"], ["AR","Arkansas"], ["CA","California"], ["CO","Colorado"], ["CT","Connecticut"], ["DE","Delaware"], ["DC","District of Columbia"], ["FL","Florida"], ["GA","Georgia"], ["HI","Hawaii"], ["ID","Idaho"], ["IL","Illinois"], ["IN","Indiana"], ["IA","Iowa"], ["KS","Kansas"], ["KY","Kentucky"], ["LA","Louisiana"], ["ME","Maine"], ["MD","Maryland"], ["MA","Massachusetts"], ["MI","Michigan"], ["MN","Minnesota"], ["MS","Mississippi"], ["MO","Missouri"], ["MT","Montana"], ["NE","Nebraska"], ["NV","Nevada"], ["NH","New Hampshire"], ["NJ","New Jersey"], ["NM","New Mexico"], ["NY","New York"], ["NC","North Carolina"], ["ND","North Dakota"], ["OH","Ohio"], ["OK","Oklahoma"], ["OR","Oregon"], ["PA","Pennsylvania"], ["RI","Rhode Island"], ["SC","South Carolina"], ["SD","South Dakota"], ["TN","Tennessee"], ["TX","Texas"], ["UT","Utah"], ["VT","Vermont"], ["VA","Virginia"], ["WA","Washington"], ["WV","West Virginia"], ["WI","Wisconsin"], ["WY","Wyoming"]] %> <select id="client_state" name="client[state]"> <% us_states.each do |state| %> <option value="<%= state[0] %>"<%= '' selected'' if state[0] == @client.state %>><%= state[1] %></option> <% end %> </select> Thanks, DAN -- Posted via http://www.ruby-forum.com/.
Tom Mornini
2006-Jul-09 09:05 UTC
[Rails] Re: Trying to create pulldown select menu for US State form
That''s too much Ruby in that their view!
Better to see this in a helper.
def select_state_for_user
[["","Select State..."],
["AL","Alabama"],
["AK","Alaska"],
["AZ","Arizona"], ["AR","Arkansas"],
["CA","California"],
["CO","Colorado"], ["CT","Connecticut"],
["DE","Delaware"], ["DC","District of
Columbia"],
["FL","Florida"],
["GA","Georgia"],
["HI","Hawaii"], ["ID","Idaho"],
["IL","Illinois"],
["IN","Indiana"], ["IA","Iowa"],
["KS","Kansas"],
["KY","Kentucky"],
["LA","Louisiana"],
["ME","Maine"], ["MD","Maryland"],
["MA","Massachusetts"],
["MI","Michigan"], ["MN","Minnesota"],
["MS","Mississippi"],
["MO","Missouri"], ["MT","Montana"],
["NE","Nebraska"],
["NV","Nevada"], ["NH","New Hampshire"],
["NJ","New
Jersey"], ["NM","New Mexico"],
["NY","New York"], ["NC","North
Carolina"], ["ND","North Dakota"],
["OH","Ohio"],
["OK","Oklahoma"],
["OR","Oregon"],
["PA","Pennsylvania"], ["RI","Rhode
Island"],
["SC","South Carolina"], ["SD","South
Dakota"],
["TN","Tennessee"],
["TX","Texas"], ["UT","Utah"],
["VT","Vermont"],
["VA","Virginia"],
["WA","Washington"], ["WV","West
Virginia"], ["WI","Wisconsin"],
["WY","Wyoming"]]
end
Then in a view:
<%= select user'', ''state'', select_state_for_user
->
In helpers/application.rb
In the controller, accessible as:
params[:user][:state]
or, hopefully,
object = User.new(params[:user])
On Jul 9, 2006, at 1:44 AM, sfdesigner wrote:
> Craig White wrote:
>> I would recommend that route...yes
>>
>
> I think I''ve got it now. Thanks for the pointer.
>
> For new and edit I have a partial _form.rhtml that is includes this...
>
> <% us_states = [["","Select State..."],
["AL","Alabama"],
> ["AK","Alaska"], ["AZ","Arizona"],
["AR","Arkansas"],
> ["CA","California"],
["CO","Colorado"], ["CT","Connecticut"],
> ["DE","Delaware"], ["DC","District of
Columbia"], ["FL","Florida"],
> ["GA","Georgia"], ["HI","Hawaii"],
["ID","Idaho"], ["IL","Illinois"],
> ["IN","Indiana"], ["IA","Iowa"],
["KS","Kansas"], ["KY","Kentucky"],
> ["LA","Louisiana"], ["ME","Maine"],
["MD","Maryland"],
> ["MA","Massachusetts"],
["MI","Michigan"], ["MN","Minnesota"],
> ["MS","Mississippi"],
["MO","Missouri"], ["MT","Montana"],
> ["NE","Nebraska"], ["NV","Nevada"],
["NH","New Hampshire"], ["NJ","New
> Jersey"], ["NM","New Mexico"],
["NY","New York"], ["NC","North
> Carolina"], ["ND","North Dakota"],
["OH","Ohio"], ["OK","Oklahoma"],
> ["OR","Oregon"],
["PA","Pennsylvania"], ["RI","Rhode
Island"],
> ["SC","South Carolina"], ["SD","South
Dakota"], ["TN","Tennessee"],
> ["TX","Texas"], ["UT","Utah"],
["VT","Vermont"], ["VA","Virginia"],
> ["WA","Washington"], ["WV","West
Virginia"], ["WI","Wisconsin"],
> ["WY","Wyoming"]] %>
>
> <select id="client_state" name="client[state]">
> <% us_states.each do |state| %>
> <option value="<%= state[0] %>"<%= ''
selected'' if state[0] => @client.state %>><%= state[1]
%></option>
> <% end %>
> </select>
>
> Thanks,
> DAN
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
sfdesigner
2006-Jul-09 17:34 UTC
[Rails] Re: Re: Trying to create pulldown select menu for US State f
Thanks - I''ve placed the array in my application_helper.rb as a def since I''m sure I''ll be using this in other areas as I develop more. I''m also using selects more for other data points and it''s all much cleaner now. Using the rails select statement is nice since it automagically adds selected="selected" to an option tag in the edit view while still defaulting to the first option in the new view. It also highlights the select menu pulldown if I try to submit the form and the model validation isn''t met such as with "validates_presence_of :state". Nice. I did need to switch the order of the abbreviation, statename to statename, abbreviation to get the select option values set to the abbreviation instead of the statename the way I wanted while showing the statenames for users to choose from. Does adding the params or object statement to application.rb add some other flexibility? Thanks again, DAN -- Posted via http://www.ruby-forum.com/.
Craig White
2006-Jul-09 18:12 UTC
[Rails] Re: Re: Trying to create pulldown select menu for US State f
On Sun, 2006-07-09 at 19:34 +0200, sfdesigner wrote:> Thanks - I''ve placed the array in my application_helper.rb as a def > since I''m sure I''ll be using this in other areas as I develop more. I''m > also using selects more for other data points and it''s all much cleaner > now. > > Using the rails select statement is nice since it automagically adds > selected="selected" to an option tag in the edit view while still > defaulting to the first option in the new view. It also highlights the > select menu pulldown if I try to submit the form and the model > validation isn''t met such as with "validates_presence_of :state". Nice. > > I did need to switch the order of the abbreviation, statename to > statename, abbreviation to get the select option values set to the > abbreviation instead of the statename the way I wanted while showing the > statenames for users to choose from. > > Does adding the params or object statement to application.rb add some > other flexibility?---- application_helper.rb is avaiable to all views, application.rb is available to all models/controllers/views Craig
Chris Carter
2006-Jul-10 04:04 UTC
[Rails] Re: Re: Re: Trying to create pulldown select menu for US Sta
Craig White wrote:> On Sun, 2006-07-09 at 19:34 +0200, sfdesigner wrote: >> >> I did need to switch the order of the abbreviation, statename to >> statename, abbreviation to get the select option values set to the >> abbreviation instead of the statename the way I wanted while showing the >> statenames for users to choose from. >> >> Does adding the params or object statement to application.rb add some >> other flexibility? > ---- > application_helper.rb is avaiable to all views, application.rb is > available to all models/controllers/views > > CraigHi, Application.rb is only availablt to controllers and views, not models. Chris -- Posted via http://www.ruby-forum.com/.