0

I've built out a fairly complex Rails (2.3.8) app with a lot of jQuery ajax requests. There is an occasional bug, which I have difficultly replicating, where a jQuery $.ajax({..}) request will request a page it shouldn't (like the dash page, which is never called with an ajax request)...

What ensures is absolutely madness. Incredibly strange and terrible errors happen.

For at least a stopgap solution (the app is in production), how can I set up a before filter than will detect any unsolicited xhr/ajax request (or ANY such request on the given controller actions) and kill it before it hits the controller?

1 Answer 1

2

In any controller:

before_filter :stop_ajax, :only => [:dashboard]

application_controller.rb:

def stop_ajax
  if request.xhr?
    render :file => "#{Rails.root}/public/404.html", :status => 404
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Change :dashboard to the name of the action(s) being invoked by your rogue AJAX and make the stop_ajax method private in the application_controller.rb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.