简体   繁体   中英

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

Ok Im like 1 hour trying to figure this out...

I have this action in my 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

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

This is what I have in my ajax call

$ ->
  $(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

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

well after I try to execute the ajax I get this in the logs

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. 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. You need to take a look in your controller either oferts_controller.rb or application_controller.rb . There must be some before_filter that is instead redirecting it to 404.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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