Forum: Ruby on Rails drop downs

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brian N. (Guest)
on 2006-04-26 16:17
I'm having trouble getting a second drop down populated based on the
value from the first drop down. I've tried following some of the
examples posted, but haven't had any success. I'm new to RoR so any help
would be appreciated. When the first drop down's value gets changed, it
calls the correct method in the controller and even finds the correct
country areas, but the results are not getting displayed in the partial
- the second drop down just remains blank.
Thanks for the help!

************* Controller
  def new
    @trip = Trip.new
    @countries = Country.find(:all)
    @country_areas = []
  end
  def find_areas_for_country
    @country_areas = CountryArea.find(:all, :conditions => ["country_id
= :country", params])
    render(:partial => "country_area", :collection =>  @country_areas)
  end

*********** form
  <tr>
    <td><label for="country">Country</label></td>
 <td><select id="country" name="country"><%=
options_from_collection_for_select(@countries, "id", "descr") %>
     </select></td>
  </tr>
  <%= observe_field(:country,
                 :update => "country_area",
                 :url => { :action => :find_areas_for_country },
                 :with => "'country='+value") %>
  <tr>
    <td><label for="country_area">Area</label></td>
    <td><%= render :partial => 'country_area' %></td>
  </tr>

********** partial
<select id="country_area" name="country_area">
<%= options_from_collection_for_select(@country_areas, "id", "descr") %>
</select>
Alan F. (Guest)
on 2006-04-26 17:15
Brian N. wrote:
> *********** form
>   <tr>
>     <td><label for="country">Country</label></td>
>  <td><select id="country" name="country"><%=
> options_from_collection_for_select(@countries, "id", "descr") %>
>      </select></td>
>   </tr>
>   <%= observe_field(:country,
>                  :update => "country_area",
>                  :url => { :action => :find_areas_for_country },
>                  :with => "'country='+value") %>
>   <tr>
>     <td><label for="country_area">Area</label></td>
>     <td><%= render :partial => 'country_area' %></td>
>   </tr>
>
> ********** partial
> <select id="country_area" name="country_area">
> <%= options_from_collection_for_select(@country_areas, "id", "descr") %>
> </select>

You don't seem to have a div or span with an id of country_area.  tthe
:update => ... bit of the observe_field is looking to replcace the
contents of an element with the results of the action, you need a div to
render into.

Alan.
Alan F. (Guest)
on 2006-04-26 17:17
Alan F. wrote:

> You don't seem to have a div or span with an id of country_area.  tthe
> :update => ... bit of the observe_field is looking to replcace the
> contents of an element with the results of the action, you need a div to
> render into.

Jyst noticed the countray_area id is used inside the partial.  this
won't work as the particl needs rendered *into* country_area.  In your
case, that should probably be the td(*).

Alan
(*)But be wary of DOM manipulation and tables in IE
Guest (Guest)
on 2006-04-27 06:24
Alan F. wrote:
> Alan F. wrote:
>
>> You don't seem to have a div or span with an id of country_area.  tthe
>> :update => ... bit of the observe_field is looking to replcace the
>> contents of an element with the results of the action, you need a div to
>> render into.
>
> Jyst noticed the countray_area id is used inside the partial.  this
> won't work as the particl needs rendered *into* country_area.  In your
> case, that should probably be the td(*).
>
> Alan
> (*)But be wary of DOM manipulation and tables in IE

Thanks that seems to work! I also had to change the line in the
controller to use :object rather than :collection

    render(:partial => "country_area", :object =>  @country_areas)

Thanks again
This topic is locked and can not be replied to.