title: rails 3 dynamic select field with jQuery
body: <p>using this example on dynamic list  <a href="https://github.com/sandeepleo11/Dynamic-Select-Menus-in-Rails-3" rel="nofollow">https://github.com/sandeepleo11/Dynamic-Select-Menus-in-Rails-3</a> I managed to get dynamic select in my form where I add a car, so when I select a carname in the next select field I get only car models that belongs to car I selected, Workin fine in this form.</p>
  
  <p>So I decided to get this thing also in my search form, you select a car and based on that you select a car model and get the results. here is the website with search form <a href="http://ec2-107-22-183-238.compute-1.amazonaws.com/cars" rel="nofollow">http://ec2-107-22-183-238.compute-1.amazonaws.com/cars</a>.</p>
  
  <p>the problem here is that when I select a car I get <code>ActionController::RoutingError (No route matches "/dynamic_search/6"):</code> in console, 6 means I picked a car name who's id is 6 and the select field for carmodels displays all models available.</p>
  
  <p>here is some code I have for the search form and dynamic search:</p>
  
  <p><strong>_search.html.erb</strong></p>
  
  <pre><code>    &lt;%= form_for @search do |f| %&gt;
      &lt;%= f.label :carname_id_equals, "Select Car Make" %&gt;
      &lt;%= f.collection_select :carname_id_equals, Carname.order('name ASC').all,  :id, :name, :include_blank =&gt; 'All' %&gt;
      &lt;%= f.label :carmodel_id_equals, "Select Model" %&gt;
      &lt;%= f.collection_select :carmodel_id_equals, Carmodel.order('name ASC').all, :id, :name, :include_blank =&gt; 'All' %&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p><strong>dynamic_search.js.erb</strong></p>
  
  <pre><code>$('#search_carmodel_id').empty();
  &lt;% for carmodel in @carmodels %&gt;
  
  
     // alert(&lt;%= carmodel.id %&gt;);
  
        $('#search_carmodel_id').append($("&lt;option&gt;&lt;/option&gt;").attr("value",&lt;%= carmodel.id %&gt;).text('&lt;%= carmodel.name %&gt;'));
  
  
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>routes.rb</strong></p>
  
  <pre><code>post "dynamic_carmodels/:id" =&gt; "cars#dynamic_search"
  </code></pre>
  
  <p><strong>controller</strong></p>
  
  <pre><code>def dynamic_search
      @carmodels = Carmodel.find_all_by_carname_id(params[:id])
  
      respond_to do |format|
        format.js
      end
    end
  </code></pre>
  
  <p><strong>application.js</strong></p>
  
  <pre><code>jQuery(document).ready(function() {
  
  //    jQuery('#search_carmodel_id_equals').html("&lt;option value=''&gt;Select Carmodel&lt;/option&gt;");
      jQuery('#search_carname_id_equals').change(function() {
  
          var data=$('#search_carname_id_equals').val();
          $.ajax({
  
              type: "POST",
               url: "http://"+location.host+"/dynamic_search/"+data,
              data: data,
  
              beforeSend: function()
              {
                  //      alert('hi');
                  //$('#status').html('&lt;img src="loading.gif"&gt;');
  
              },
  
              success: function(response)
              {
                   //  alert(response);
                  //  $('#search_carmodel_id_equals').html(html);    //dynamic_search.js.erb
                     //      $('#status').html(html);
  
  
              }
  
          });
      });
  });
  </code></pre>