Note that there are some explanatory texts on larger screens.

plurals
  1. POError 404 for Dynamic Dropdown with rails 3.1 and Ajax (Jquery)
    primarykey
    data
    text
    <p>I am having trouble on creating dynamic dropdowns where: </p> <p>When you select a country, it renders the partial with cities that belong to a country. </p> <p>I have the models Country has_many cities and City belongs_to Country</p> <p>With firebug I get an Error when fetching the controller action update_city_select, where it selects a Contact record instead of Country record.</p> <p>I followed the example from <a href="http://www.petermac.com/rails-3-jquery-and-multi-select-dependencies/" rel="nofollow">Peter Mac</a></p> <p>Here is my code:</p> <p>application.js</p> <pre><code>jQuery(function($) { // when the #region_id field changes $("#contact_country_id").live('change', function() { // make a POST call and replace the content var country = $('select#contact_country_id :selected').val(); if(country == "") country="0"; jQuery.get('/contacts/update_city_select/' + country, function(data){ $("#cities").html(data); }) return false; }); }) </code></pre> <p>contacts_controller.rb</p> <pre><code> def new @contact = Contact.new @industries = Industry.all @countries = Country.all @cities = City.where(["country_id = ?", 1]).all respond_to do |format| format.html # new.html.erb format.json { render json: @contact } end end . . . def update_city_select @cities = City.where(:country_id =&gt; params[:id]).order(:name) unless params[:id].blank? render :partial =&gt; "cities", :locals =&gt; { :cities =&gt; @cities } end </code></pre> <p>contacts/_form.html.erb</p> <pre><code>&lt;%= form_for(@contact) do |f| %&gt; ... &lt;div class="field"&gt; &lt;%= f.label :country %&gt; &lt;%= f.collection_select :country_id, @countries, :id, :name, :prompt =&gt; "-- Select a country --" %&gt; &lt;/div&gt; &lt;div id="cities" class="field"&gt; &lt;%= render 'cities' %&gt; &lt;/div&gt; ... &lt;% end %&gt; </code></pre> <p>contacts/_cities.html.erb</p> <pre><code>&lt;%= fields_for @contact do |f| %&gt; &lt;%= f.label :city %&gt; &lt;% unless cities.blank? %&gt; &lt;%= f.collection_select :city_id, cities, :id, :name, :prompt =&gt; "-- Select a city --" %&gt; &lt;% else %&gt; &lt;%= f.select "city_id","city_id", :prompt =&gt; "-- Select a city --" %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>routes.rb</p> <pre><code> resources :cities resources :countries resources :contacts get '/contacts/update_city_select/:id' =&gt; 'contacts#update_city_select' </code></pre> <p>You're help on solving this Ajax problem would be most appreciated!</p> <p>Aurelien</p>
    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