# Problem with AJAX Picklists on Edit

**URL:** <https://www.ruby-forum.com/t/problem-with-ajax-picklists-on-edit/112152>\
**Category:** Rails\
**Created:** [August 15, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/problem-with-ajax-picklists-on-edit/112152 "2007-08-15T23:16:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scotdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@scotdb](https://www.ruby-forum.com/u/scotdb)\
**Post date:** [August 15, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/problem-with-ajax-picklists-on-edit/112152/1 "2007-08-15T23:16:54Z")

</div>

Folks,

I’m using a couple of picklists, populated from database tables, to  
take in Country and State in a form. The Countries and States models  
are in a belongs\_to / has\_many relationship, but not all countries  
have states available in a picklist (I have them for US and Canada,  
but not for the rest of the world). When this happens, I set the  
state\_id to null and present a textbox for the user to type in the  
state.

Anyway, it works fine on initial data entry. However when I use the  
same partial to perform an edit on an existing row the information for  
the state isn’t being shown correctly (it always shows the first state  
in the US list).

Here’s (part of) my \_form.rhtml -

========== START OF CUT ==================

Country  
\<%= @recipient\_countries = Country.find(:all, :order =\> "display\_priority,country\_name") collection\_select(:order, :recipient\_country\_id, @recipient\_countries, :id, :displayname, options = {:prompt =\> "- Select Country -"}, html\_options = { :onChange =\> "new Ajax.Updater('recipient\_states', '/order/recipient\_country\_changed/' + this[this.selectedIndex].value, {asynchronous:true, evalScripts:true});" } ) %\>

 \<%= render :partial =\> "recipient\_states", :locals =\> {:recipient\_country\_id =\> @recipient\_countries[0].id} %\> ================ END OF CUT ================
## And here is the method “recipient\_country\_changed” from the controller

=============== START OF CUT ===================  
def recipient\_country\_changed  
render :partial =\> “recipient\_states”, :locals =\>  
{:recipient\_country\_id =\> params[:id]}  
end  
============== END OF CUT ===================

And finally here is the partial \_recipient\_states.rhtml -

=============== START OF CUT =====================

\<% if State.exists?(["country\_id = ?", recipient\_country\_id]) %\> State  
\<%= collection\_select(:order, :recipient\_state\_id, State.find(:all, :conditions =\> ["country\_id = ?", recipient\_country\_id]), :id, :state\_name, options = {:prompt =\> "-- Select a State --"} ) %\> \<% else %\> State  
\<%= text\_field 'order', 'recipient\_state\_text', :size =\> 20 %\> \<% end %\>

================ END OF CUT =========================

If anyone can suggest a fix for this I’d be very grateful.

Thanks

Phil Nelson

---

<div class="post-metadata">

**Author:** ![scotdb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@scotdb](https://www.ruby-forum.com/u/scotdb)\
**Post date:** [August 17, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/problem-with-ajax-picklists-on-edit/112152/2 "2007-08-17T23:52:49Z")

</div>

OK,

I got that working … it seems that just coding -

:locals =\> {:recipient\_country\_id =\> @recipient\_countries.id}

in place of

:locals =\> {:recipient\_country\_id =\> @recipient\_countries[0].id}

does the trick.

But when I used the same technique further down in my code I started  
running  
into an issue, where this was sending through a large negative number  
when  
the screen was first displayed (I printed the value), but then when I  
used  
the drop boxes thereafter everything was fine. It is as if when I first  
display the form that this particular value isn’t getting set up  
correctly.

Any clues ?

Thanks

Phil
