【发布时间】:2016-06-27 21:20:48
【问题描述】:
我使用的是 Rails 4.2.3。我有一个控制器,只有一种方法
class CountriesController < ApplicationController
def states
@country = Country.find params[:country_id]
@states = @country.states
respond_to do |format|
format.json { render json: @states.to_json }
end
end
end
在我设置的 config/routes.rb 文件中
resources :countries do
get :state, on: :member #-> url.com/countries/:country_id/states/
end
但是,当我访问 URL 时
http://mydomein.devbox.com:3000/countries/38/states
我收到 404。我还需要做什么才能让它工作?
编辑:我编辑了我的咖啡脚本以匹配建议(添加内容类型),但这仍然导致 404 ...
@update_states = (countryElt, stateElt) ->
url = "/countries/" + $(countryElt).val() + "/states"
$.ajax
url: url
type: 'GET'
contentType: 'application/json'
success: (data) ->
for key, value of data
$(stateElt).find('option').remove().end()
$(stateElt).append('<option value=' + key + '>' + value + '</option>')
【问题讨论】:
标签: ruby-on-rails-4 routes config restful-url