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.

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

Craig W. 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”]] %>

<% us_states.each do |state| %> ><%= state[1] %> <% end %>

Thanks,
DAN

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

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 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

Craig

Craig W. 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

Craig

Hi,
Application.rb is only availablt to controllers and views, not models.
Chris

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs