2011-11-19 57 views
0

在動態列表中使用此示例https://github.com/sandeepleo11/Dynamic-Select-Menus-in-Rails-3我設法在我的表單中添加動態選擇,因此當我在下一個選擇字段中選擇汽車名稱時,我只獲取屬於我選擇的車,Workin罰款這種形式。rails 3使用jQuery動態選擇字段

所以我決定在我的搜索表單中找到這個東西,你選擇一輛汽車並根據你選擇一輛汽車模型並獲得結果。這裏是搜索表單http://ec2-107-22-183-238.compute-1.amazonaws.com/cars的網站。

的這裏的問題是,當我選擇一輛車,我得到ActionController::RoutingError (No route matches "/dynamic_search/6"):控制檯,6是指我挑一個車名誰的ID是6和carmodels顯示所有型號選擇欄。

這裏是一些代碼,我對搜索表單和動態搜索:

_search.html.erb

<%= form_for @search do |f| %> 
    <%= f.label :carname_id_equals, "Select Car Make" %> 
    <%= f.collection_select :carname_id_equals, Carname.order('name ASC').all, :id, :name, :include_blank => 'All' %> 
    <%= f.label :carmodel_id_equals, "Select Model" %> 
    <%= f.collection_select :carmodel_id_equals, Carmodel.order('name ASC').all, :id, :name, :include_blank => 'All' %> 
    <% end %> 

dynamic_search.js.erb

$('#search_carmodel_id').empty(); 
<% for carmodel in @carmodels %> 


    // alert(<%= carmodel.id %>); 

     $('#search_carmodel_id').append($("<option></option>").attr("value",<%= carmodel.id %>).text('<%= carmodel.name %>')); 


<% end %> 

routes.rb

post "dynamic_carmodels/:id" => "cars#dynamic_search" 

控制器

def dynamic_search 
    @carmodels = Carmodel.find_all_by_carname_id(params[:id]) 

    respond_to do |format| 
     format.js 
    end 
    end 

的application.js

jQuery(document).ready(function() { 

// jQuery('#search_carmodel_id_equals').html("<option value=''>Select Carmodel</option>"); 
    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('<img src="loading.gif">'); 

      }, 

      success: function(response) 
      { 
       // alert(response); 
       // $('#search_carmodel_id_equals').html(html); //dynamic_search.js.erb 
        //  $('#status').html(html); 


      } 

     }); 
    }); 
}); 

回答

0

解決:

路由有一個問題:

post "dynamic_carmodels/:id" => "cars#dynamic_search" 

應該

post "dynamic_search/:id" => "cars#dynamic_search" 

dynamic_search.js.erb

$('#search_carmodel_id_equals').empty(); 
<% for carmodel in @carmodels %> 


    // alert(<%= carmodel.id %>); 

     $('#search_carmodel_id_equals').append($("<option></option>").attr("value",<%= carmodel.id %>).text('<%= carmodel.name %>')); 


<% end %>strong text