With the following code :
class TestsController < ApplicationController
skip_before_filter :load_something, # how to skip these only
:load_something_else # if the request is xhr ?
def index
respond_to do |format|
format.html
format.js
end
end
end
Is there a way to skip before filters depending if the request is a javascript call without changing the :load_something and :load_something_else methods ?
Thanks !
There used to be no clean solution to this. Assuming you do not have control over the
before_filters(or do not want to change them), i would leave theskip_before_filterand add an extrabefore_filterthat only executes the wanted methods if it is anxhrmethod.So something like
The method described here only works because the conditional is global for the application: the conditional is only evaluated when the class is created. Not on each request.
[UPDATED]
There is however, a cleaner way to make the
skip_before_filterconditional. Write it like this:Also note that while
skip_before_filteris still completely supported, and not deprecated, since rails 4 the documentation seems to prefer the (identical)skip_before_action. Also the documentation is totally not clear about this, had to verify in code.