简体   繁体   English

Rails 4:找不到对控制器操作的ajax调用,返回404

[英]Rails 4: ajax call to controller action returning 404 not found

Ok Im like 1 hour trying to figure this out... 好的,我大概需要1个小时才能弄清楚这一点...

I have this action in my oferts_controller.rb 我的oferts_controller.rb中有此操作

    def update_categories
     @categories = Category.children_of(Category.find(params[:categories]))
     respond_to do |format|
      format.js
     end
    end

This is what I have in routes.rb 这就是routes.rb中的内容

get 'oferts/update_categories', as: 'update_categories'

This is what I have in my ajax call 这就是我的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!")

it is suposed that the update_categories action responds as a JavaScript, which is in app/views/oferts/update_categories.js.coffee 假设update_categories操作以JavaScript的形式响应,它位于app / views / oferts / update_categories.js.coffee中

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

well after I try to execute the ajax I get this in the logs 在我尝试执行ajax之后,在日志中得到了这个

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)

Well if you see it is not even entering to the controller action, I tried adding puts inside the update_categories def in the controller but those are not printed in the logs. 好吧,如果您发现它甚至没有进入控制器操作,我尝试在控制器的update_categories def内添加puts ,但这些未打印在日志中。 is this routing problem? 这是路由问题吗?

Thanks for you help. 感谢您的帮助。

I have once got this problem 我曾经有这个问题

finally, I find that the problem is in the filter 最后,我发现问题出在过滤器中

Could you have any filer in your controller and would you like post it for help? 您的控制器中可以有任何文件管理器,您想将其发布以寻求帮助吗?

The issue exists in not allowing such ajax methods in controller to pass even without any validation or authentication. 问题在于,即使没有任何验证或身份验证,也不允许控制器中的此类ajax方法通过。 You need to take a look in your controller either oferts_controller.rb or application_controller.rb . 您需要查看控制器oferts_controller.rb或application_controller.rb There must be some before_filter that is instead redirecting it to 404. 必须有一些before_filter替代将其重定向到404。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM