# Contingent Select Boxes - 2 Q's

**URL:** <https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930>\
**Category:** Rails\
**Created:** [May 27, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930 "2009-05-27T13:38:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/1 "2009-05-27T13:38:01Z")

</div>

I have a form with contingent select boxes (the state is contingent on  
the country selected, so when the country selected changes, the state  
changes – I am using the Carmen plugin for getting my state names and  
country names together, but not the functionality I am interested in  
achieving).

Everything works fine except I cannot figure out how to amend my code  
such that:  
A. When the form is first rendered the state select box is empty (even  
if there is a country showing, and the user (what I am calling an  
“Associate” in terms of the model) HAS a state selected in his model. -  
and-  
B.If I have, say, a state selected, I change the country, don’t select  
a state for that country, then go back and select the original country  
(for which I have a state selected) it still doesn’t display the  
selected state – just the list of states, unselected, in the state  
select box (I believe the solution to #1, above, should solve this  
too).

Ive scoured the net for examples of related matters, but cannot find  
one similar to this. I am certain the solution must be little more  
than a few lines of code which I am entirely oblivious to. Can someone  
help me here? -JannaB

Here is the code I am using:

First, I have a model called Associates (for simplicity in  
demonstration here, the only two members of Associates are state and  
country, both Strings), and the /views/associates/\_form.html.erb, as  
follows:

\<% form\_for @associate do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :city %\>  
 \<%= f.text\_field :city %\>

\<%= f.label :state %\>  
 \<%= render :partial =\> 'get\_states' %\>

\<%= f.label :country %\>  
 \<%= f.country\_select :country, Carmen.default\_country, {}, { :onChange =\> remote\_function( :url =\> { :action =\> '\_get\_states' }, :update =\> "state", :with =\> "'country=' +value") } %\>

\<%= f.submit "Submit" %\>

\<% end %\>

"  
Note that I have a partial that gets called, /views/associates/  
\_get\_states.html.erb:

\<% if @states !=nil %\>  
\<% for state in @states %\>

 \<%= state[0] %\> \<% end %\> \<% end %\>

Then, in my AssociatesController, I put in the method \_get\_states:

def _get\_states  
 #essentially, this requires the 2 letter code for the country  
selected, so if ‘united\_kingdom’ is selected  
 #it converts to UK and gets its “states”. It also handles the default,  
which is already specified properly  
q = params[:country]  
if(q!=Carmen.default\_country)  
q1 = q.gsub!("_", " ")  
if(q1!=q && q1!=nil)  
q = q1.titleize  
else  
q.capitalize!  
end  
q = Carmen::country\_code(q)  
end  
@states = Carmen::states(q)  
end

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 2:09pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/2 "2009-05-27T14:09:10Z")

</div>

On May 27, 12:37 pm, JannaB [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “Associate” in terms of the model) HAS a state selected in his model. -  
> and-

A select box has a selected value if one of its option tags has  
selected=“selected”. You never do this to you option tags and so your  
select box will never show the selected value. Why not use one of the  
rails helpers for generating option tags ?

Fred

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 2:12pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/3 "2009-05-27T14:12:41Z")

</div>

Fred,

What do you mean “use one of the rails helpers for generating option  
tags?” Where can I see an example of this? Thanks, Janna B

On May 27, 8:08 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/4 "2009-05-27T14:32:43Z")

</div>

Yes, I understand – and am looking at it – Im just not sure where to  
cobble it in (in what files, amending what files) given what I have  
working here. -JannaB

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/5 "2009-05-27T14:17:24Z")

</div>

On 27/05/2009, at 10:12 PM, JannaB wrote:

> Fred,
> 
> What do you mean “use one of the rails helpers for generating option  
> tags?” Where can I see an example of this? Thanks, Janna B

[api.rubyonrails.org](http://api.rubyonrails.org) has examples.

options\_for\_select (ActionView::Helpers::FormOptionsHelper)

options\_from\_collection\_for\_select  
(ActionView::Helpers::FormOptionsHelper)

Julian.

* * *

Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 2:41pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/6 "2009-05-27T14:41:36Z")

</div>

On 27/05/2009, at 10:31 PM, JannaB wrote:

> Yes, I understand – and am looking at it – Im just not sure where to  
> cobble it in (in what files, amending what files) given what I have  
> working here. -JannaB

You know what I really like to do? put a debugger into your view, and  
then mess with the method in the console… it’s good. Go do it now…  
(just don’t forget to take out the debugger later)

Just now I wacked a \<% debugger %\> into the layout of one of my apps,  
and loaded up mongrel, then hit the root url…

bam… up pops a console, and you’re “in” a view… you can mess with  
it… you get stack traces if you write the wrong thing, but it’s good  
all the same…

Phatty:sensei julian$ mongrel\_rails start -p 3003  
\*\* Starting Mongrel listening at 0.0.0.0:3003  
\*\* Starting Rails with development environment…  
\*\* Rails loaded.  
\*\* Loading any Rails specific GemPlugins  
\*\* Signals ready. TERM =\> stop. USR2 =\> restart. INT =\> stop (no  
restart).  
\*\* Rails signals registered. HUP =\> reload (without restart). It  
might not work well.  
\*\* Mongrel 1.1.5 available at 0.0.0.0:3003  
\*\* Use CTRL-C to stop.

---- HERE IS WHERE I LOADED THE ROOT OF MY APP IN A BROWSER: localhost:  
3003 –

/Users/julian/code/rails/sensei/app/views/layouts/application.html.erb:  
15  
\<% debugger %\>  
(rdb:1) irb

> > options\_for\_select(“hi”)  
> > =\> “\<option value="hi"\>hi”  
> > options\_for\_select([1])  
> > =\> “\<option value="1"\>1”  
> > options\_for\_select([1,2])  
> > =\> “\<option value="1"\>1\n\<option value="2"\>2”  
> > options\_for\_select([1,2,3])  
> > =\> “\<option value="1"\>1\n\<option value="2"\>2  
> > \n\<option value="3"\>3”  
> > options\_for\_select([[1,2],[3,4]])  
> > =\> “\<option value="2"\>1\n\<option value="4"\>3”  
> > options\_for\_select([[:hello,2],[:yes,4]])  
> > =\> “\<option value="2"\>hello\n\<option value="4"\>yes\</  
> > option\>”  
> > options\_for\_select  
> > ArgumentError: wrong number of arguments (0 for 1)  
> > from (irb):2:in `options_for_select' from (irb):2:in `send’  
> > from /Users/julian/code/rails/sensei/app/views/layouts/  
> > application.html.erb:15:in `send' from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/ action_view/renderable.rb:34:in `render’  
> > from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/  
> > action\_view/base.rb:301:in `with_template' from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/ action_view/renderable.rb:30:in `render’  
> > from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/  
> > action\_view/template.rb:194:in `render_template' from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/ action_view/base.rb:260:in `render’  
> > from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/  
> > action\_view/base.rb:347:in `_render_with_layout' from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/ action_view/base.rb:257:in `render’  
> > from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/  
> > action\_controller/base.rb:1241:in `render_for_file' from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/ action_controller/base.rb:937:in `render\_without\_benchmark’  
> > from /Users/julian/code/rails/sensei/vendor/rails/actionpack/lib/  
> > action\_controller/benchmarking.rb:51:in `render' from /Users/julian/code/rails/sensei/vendor/rails/activesupport/lib/ active_support/core_ext/benchmark.rb:17:in `ms’  
> > from /Users/julian/code/rails/sensei/vendor/rails/activesupport/lib/  
> > active\_support/core\_ext/benchmark.rb:10:in `realtime' from /Users/julian/code/rails/sensei/vendor/rails/activesupport/lib/ active_support/core_ext/benchmark.rb:17:in `ms’  
> > … 46 levels…  
> > from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:  
> > 158:in `process_client' from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb: 285:in `run’  
> > from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:  
> > 285:in `initialize' from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb: 285:in `new’  
> > from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:  
> > 285:in `run' from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb: 268:in `initialize’  
> > from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb:  
> > 268:in `new' from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel.rb: 268:in `run’  
> > from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/  
> > configurator.rb:282:in `run' from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/ configurator.rb:281:in `each’  
> > from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/  
> > configurator.rb:281:in `run' from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails: 128:in `run’  
> > from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/  
> > command.rb:212:in `run' from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281 from /usr/bin/mongrel_rails:19:in `load’  
> > from /usr/bin/mongrel\_rails:19  
> > exit  
> > /Users/julian/code/rails/sensei/app/views/layouts/application.html.erb:  
> > 15  
> > \<% debugger %\>  
> > (rdb:1) cont

* * *

Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/7 "2009-05-27T15:06:53Z")

</div>

Julian - \<% debugger %\> will only work if you are running in irb, yes?  
I am running in Mongrel and I get nothing. -Janna B

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/8 "2009-05-27T17:01:02Z")

</div>

On 27/05/2009, at 11:06 PM, JannaB wrote:

> > > where to
> > 
> > bam… up pops a console, and you’re “in” a view… you can mess with  
> > it… you get stack traces if you write the wrong thing, but it’s  
> > good  
> > all the same…

No debugger will work in mongrel if you have required the debugger in  
your environments/development.rb file

require “ruby-debug”

Julian.

* * *

Learn: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)  
Last updated 20-May-09 (Rails, Basic Unix)  
Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 28, 2009, 10:32am UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/9 "2009-05-28T10:32:13Z")

</div>

Or you can start your dev server with the --debugger option

2009/5/27, Julian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > (just don’t forget to take out the debugger later)  
> > > No debugger will work in mongrel if you have required the debugger in  
> > > Twitter: [http://twitter.com/random8r](http://twitter.com/random8r)  
> > > might not work well.  
> > > \<% debugger %\>
> > > 
> > > > > options\_for\_select([[1,2],[3,4]])  
> > > > > from /Users/julian/code/rails/sensei/vendor/rails/  
> > > > > action\_view/template.rb:194:in `render_template' actionpack/lib/ from /Users/julian/code/rails/sensei/vendor/rails/ mongrel.rb: from /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/ configurator.rb:282:in `run’  
> > > > > mongrel/  
> > > > > (rdb:1) cont
> 
> >

–  
Von meinen Mobilgerät aus gesendet

---

<div class="post-metadata">

**Author:** ![JannaB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/df788c/32.png) [@JannaB](https://www.ruby-forum.com/u/JannaB)\
**Post date:** [May 27, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/contingent-select-boxes-2-qs/168930/10 "2009-05-27T15:25:07Z")

</div>

My question then boils down to this – how do I access the string  
value of Associate.state from within my partial /views/associates/  
\_get\_states,html.erb so as to perform the conditional on line 4,  
below? -Janna B (state[0] might == “NY” but the right side of the ==  
needs to be Associate.state. how do I get at that from in here?

\<% if @states !=nil %\>  
\<% for state in @states %\>  
\<option value="\<%= state[1] %\>"  
\<% if state[0]==state %\>  
selected=“selected”  
\<% end %\>  
\>  
\<%= state[0] %\>  
\<% end %\>  
\<% end %\>
