2015-11-06 54 views
0

我试图建立在Rails级联选择框。我以dynamic-select-boxes为例。出于某种原因,我无法让它正常工作。当我在下拉菜单中进行新选择时,WEBrick显示ActiveRecord的404 Not Found错误。麻烦级联Rails中

确切的错误是:

Started GET "/estimates/update_areas?product_type_id=1&_=1446822835903" for 50.17.182.190 at 2015-11-06 16:05:23 +0000 
Cannot render console from 50.17.182.190! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 
Processing by EstimatesController#show as JS 
    Parameters: {"product_type_id"=>"1", "_"=>"1446822835903", "id"=>"update_areas"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."name" ASC LIMIT 1 [["id", 1]] 
    Estimate Load (0.3ms) SELECT "estimates".* FROM "estimates" WHERE "estimates"."id" = $1 ORDER BY "estimates"."updated_at" DESC LIMIT 1 [["id", 0]] 
Completed 404 Not Found in 4ms (ActiveRecord: 0.6ms) 


ActiveRecord::RecordNotFound (Couldn't find Estimate with 'id'=update_areas): 
    app/controllers/estimates_controller.rb:9:in `show' 

出于某种原因,请求被路由到我的控制器中的“显示”方法,我想不通为什么。

我estimates_controller.rb内的update_areas方法:

def update_areas 
    @areas = Area.where("product_type_id = ?", params[:product_type_id]) 
    respond_to do |format| 
     format.js 
    end 
    end 

相关的路线是:

resources :estimates 
    get 'estimates/new/update_areas', to: 'estimates#update_areas' 

我estimate.js.coffee

$ -> 
    $(document).on 'change', '#product_type_select', (evt) -> 
    $.ajax 'update_areas', 
     type: 'GET' 
     dataType: 'script' 
     data: { 
     product_type_id: $("#product_type_select option:selected").val() 
     } 
     error: (jqXHR, textStatus, errorThrown) -> 
     console.log("AJAX Error: #{textStatus}") 
     success: (data, textStatus, jqXHR) -> 
     console.log("Dynamic area select OK!") 

和我update_areas。 js.coffee

$("#areas_select").empty().append("<%= escape_javascript(render(:partial => @areas)) %>") 

任何想法,为什么Ajax请求被发送到了错误的路线?

更新1 好吧,我还是没有解决这一点,但只是想我要补充一点的更多信息。我注释掉资源:估计从我的路线文件线和左线得到“估计/ update_areas /”,到:“估计#update_areas”在routes文件,一切工作像预期。只要我取消评估资源评估,它就会失败。

随着估算资源注释去掉,我得到这样

Parameters: {"product_type_id"=>"1", "_"=>"1447260557304", "id"=>"update_areas"} 
的WEBrick

线。没有我得到的资源

Parameters: {"product_type_id"=>"2", "_"=>"1447260557305" 

这是我所期望的。为什么当我在路由文件中添加资源行时突然传递了一个ID参数?

回答

0

好吧,我终于得到了这个工作。

在我的routes.rb估计资源变成了:

resources :estimates do 
    get 'update_areas', on: :new 
    end 

和我estimates.js.coffee变成了:

$ -> 
    $(document).on 'change', '#product_type_select', (evt) -> 
    $.ajax 
     url:'/estimates/new/update_areas', 
     type: 'GET', 
     dataType: 'script', 
     data: { 
     product_type_id: $("#product_type_select option:selected").val() 
     } 
     error: (jqXHR, textStatus, errorThrown) -> 
     console.log("AJAX Error: #{textStatus} #{errorThrown}") 
     success: (data, textStatus, jqXHR) -> 
     console.log("Dynamic area select OK!") 

此接受了适当的参数和更新的目标选择框。希望这有助于别人有一天。