# Trying to create pulldown select menu for US State form fiel

**URL:** <https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638>\
**Category:** Rails\
**Created:** [July 9, 2006, 12:24am UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638 "2006-07-09T00:24:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [July 9, 2006, 12:24am UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638/1 "2006-07-09T00:24:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [July 9, 2006, 3:38am UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638/2 "2006-07-09T03:38:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [July 9, 2006, 10:44am UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638/3 "2006-07-09T10:44:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [July 9, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638/4 "2006-07-09T19:34:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [July 9, 2006, 11:08am UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638/5 "2006-07-09T11:08:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [July 9, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638/6 "2006-07-09T20:14:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sfdesigner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c37758/32.png) [@sfdesigner](https://www.ruby-forum.com/u/sfdesigner)\
**Post date:** [July 10, 2006, 6:04am UTC](https://www.ruby-forum.com/t/trying-to-create-pulldown-select-menu-for-us-state-form-fiel/97638/7 "2006-07-10T06:04:35Z")

</div>

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
