Javascript Rails 4:ajax调用控制器操作返回404未找到
好吧,我想用一个小时来解决这个问题 我在oferts_controller.rb中有此操作Javascript Rails 4:ajax调用控制器操作返回404未找到,javascript,ruby-on-rails,ruby,ajax,ruby-on-rails-4,Javascript,Ruby On Rails,Ruby,Ajax,Ruby On Rails 4,好吧,我想用一个小时来解决这个问题 我在oferts_controller.rb中有此操作 def update_categories @categories = Category.children_of(Category.find(params[:categories])) respond_to do |format| format.js end end 这是我在routes.rb中看到的 get 'oferts/update_cat
def update_categories
@categories = Category.children_of(Category.find(params[:categories]))
respond_to do |format|
format.js
end
end
这是我在routes.rb中看到的
get 'oferts/update_categories', as: 'update_categories'
这就是我的ajax调用中的内容
$ ->
$(document).on 'change', '#categories_parents_select', (evt) ->
$.ajax 'update_categories',
type: 'GET'
dataType: 'script'
data: {
categories: $("#categories_parents_select option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic country select OK!")
建议update_categories操作以JavaScript响应,它位于app/views/oferts/update_categories.js.coffee中
$("#categories_select").empty()
.append("<%= escape_javascript(render(:partial => @categories)) %>")
如果您看到它甚至没有进入控制器操作,我尝试在控制器中的更新类别def
中添加put
,但这些没有打印在日志中。这是路由问题吗
谢谢你的帮助。我曾经遇到过这个问题 最后,我发现问题出在过滤器上
您的控制器中是否有任何文件管理器,是否希望将其发布以寻求帮助?问题在于,即使没有任何验证或身份验证,也不允许控制器中的此类ajax方法通过。您需要查看RTS\u controller.rb或application\u controller.rb的控制器。必须有一些在\u filter之前,将其重定向到404
谢谢这看起来确实像一个路由问题。在ajax调用中,尝试使用路径“oferts/update\u categories”,我这样做了,它说没有定义“oferts/oferts/update\u categories”=(我还能做什么?你能发布你的路线文件吗?非常感谢,你打开了我大脑中的一盏灯,是的,我有之前的过滤器:验证用户!并加载cancancan的资源和授权资源,我刚刚添加了除了:数组我的更新类别操作。再次感谢你,铁路的错误不是很描述性…@SsouLlesS很乐意帮助y。)你解决了这个问题。这是我的第一个最好的答案,谢谢你。
Started GET "/oferts/update_categories?categories=3&_=1427927753744" for 127.0.0.1 at 2015-04-01 17:36:01 -0500
Processing by OfertsController#update_categories as JS
Parameters: {"categories"=>"3", "_"=>"1427927753744"}
Rendered public/404.html (0.6ms)
Completed 404 Not Found in 38ms (Views: 36.8ms | ActiveRecord: 0.0ms)