动态选择导轨4和ajax上的路由错误
问题描述:
一直在处理此问题,无法弄清楚。动态选择导轨4和ajax上的路由错误
试图做出动态选择来根据状态选择城市。这是为设计用户注册页面,我认为这只是添加到路由错误。
Ajax代码:(资产/ Java脚本/ registration.js.coffee)
$ ->
$(document).on 'change', '#user_state_id', (evt) ->
$.ajax '/registrations/update_cities',
type: 'GET'
dataType: 'script'
data: {
state_id: $("#user_state_id option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic state select OK!")
控制器(registrations_controller.rb)
def edit
@states = State.all
@cities = Cities.where("state_id = ?", State.first.id)
end
def update_cities
@cities = Cities.where("state_id = ?", params[:state_id])
respond_to do |format|
format.js
end
end
更新JS:(视图/设计/注册/ update_cities。 js.coffee)
$("#user_city_id").empty().append("<%= escape_javascript(render(:partial => @cities)) %>")
共用部分(视图/共享/ _city.html.erb)
<option value="<%= city.id %>"><%= city.name.titleize %></option>
路线(供参考)
devise_for :users, controllers: {registrations: 'registrations'} do
get 'registrations/update_cities', as: 'update_cities'
end
误差(在日志)
ActionController::RoutingError (No route matches [GET] "/registrations/update_cities"):
rake routes
输出
states GET /states(.:format) states#index
POST /states(.:format) states#create
new_state GET /states/new(.:format) states#new
edit_state GET /states/:id/edit(.:format) states#edit
state GET /states/:id(.:format) states#show
PATCH /states/:id(.:format) states#update
PUT /states/:id(.:format) states#update
DELETE /states/:id(.:format) states#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
dashboards_list_zip GET /dashboards/list_zip(.:format) dashboards#list_zip
authenticated_root GET / dashboards#index
root GET / devise/sessions#new
任何帮助理解。我正在尝试执行如下所示的操作:Kernal Garden;修改设计和州/市级。
答
得到它的工作是这样的: registration.js.coffee:
$ ->
$(document).on 'change', '#user_state_id', (evt) ->
$.ajax '/registrations/update_cities',
type: 'GET'
dataType: 'script'
data: {
state_id: $("#user_state_id option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic state select OK!")
注册控制器:
def edit
@states = State.all
if current_user.state_id.nil?
@cities = City.where("state_id = ?", State.first.id)
else
@cities = City.where("state_id = ?", current_user.state_id)
end
end
def update_cities
@cities = City.where("state_id = ?", params[:state_id])
respond_to do |format|
format.js
end
end
感谢您的帮助!
答
您的registration.js.coffee
coffeescript未呈现,因此它将无法使用update_cities
的路由帮助程序。相反,它将路径视为相对于当前路径,因此它将变为/users/update_cities
。
改变这一行:
$.ajax 'update_cities',
这样:
$.ajax '/registrations/update_cities',
让我们来试试这个路线:
devise_for :users, controllers: {registrations: 'registrations'} do
end
get '/users/update_cities', to: :registrations, as: 'update_cities'
它可能需要把URL回到你有什么它之前,但这应该给你一个路线update_cities
你还可以显示'rake routes'的结果吗? –
已添加。注意到基于新范围的update_cities不存在了 – matthewvb
奇数。我在'rake routes'输出中看不到update_cites。它是否被裁剪,还是不创建路线? –