我試圖建立在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參數?