Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This <a href="http://pullmonkey.com/2008/03/30/dynamic-select-boxes-ruby-on-rails/" rel="nofollow">link</a> may help you in doing it.</p> <p><strong>Rails 2 + Prototype</strong></p> <p><em>app/models/cities.rb</em></p> <pre><code>class City &lt; ActiveRecord::Base has_many :regions has_many :reports, :through =&gt; :regions # this is newly added end </code></pre> <p><em>cities_controller</em></p> <pre><code>class CitiesController &lt; ApplicationController def index @cities = City.find(:all) @regions = Region.find(:all) @reports = Report.find(:all) end def update_regions # updates regions and reports based on city selected city = City.find(params[:city_id]) regions = city.regions reports = city.reports render :update do |page| page.replace_html 'regions', :partial =&gt; 'regions', :object =&gt; regions page.replace_html 'reports', :partial =&gt; 'reports', :object =&gt; reports end end def update_reports # updates reports based on region selected region = Region.find(params[:region_id]) reports = region.reports render :update do |page| page.replace_html 'reports', :partial =&gt; 'reports', :object =&gt; reports end end end </code></pre> <p><em>_reports.html.erb</em></p> <pre><code>&lt;%= collection_select(nil, :report_id, reports, :id, :title, {:prompt =&gt; "Select a Report"}) %&gt; </code></pre> <p><em>_regions.html.erb</em></p> <pre><code>&lt;%= collection_select(nil, :region_id, regions, :id, :name, {:prompt =&gt; "Select a Region"}, {:onchange =&gt; "#{remote_function(:url =&gt; {:action =&gt; "update_reports"}, :with =&gt; "'region_id='+value")}"}) %&gt; &lt;br/&gt; </code></pre> <p><em>index.html.erb</em></p> <pre><code>&lt;%= javascript_include_tag :defaults %&gt; &lt;%= collection_select(nil, :city_id, @cities, :id, :name, {:prompt =&gt; "Select a City"}, {:onchange =&gt; "#{remote_function(:url =&gt; {:action =&gt; "update_regions"}, :with =&gt; "'city_id='+value")}"}) %&gt; &lt;br/&gt; &lt;div id="regions"&gt;&lt;%= render :partial =&gt; 'regions', :object =&gt; @regions %&gt;&lt;/div&gt; &lt;div id="reports"&gt;&lt;%= render :partial =&gt; 'reports', :object =&gt; @reports %&gt;&lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload