In a controller in my Rails app, I can do this:
before_filter :login_required, :except => :index
But I would like to apply the filter not only based on the action name but on the format of the request. In other words, I would like to do something like this:
before_filter :login_required, :except => {:action => :index, :format => :js}
Is this possible?
You'll need to roll your own a bit. Try this as a starting point.
before_filter :login_required, :except => [:index]
before_filter(:only => :index) do |controller|
login_required unless controller.request.format.js?
end
Remember, you can extract the lambda to a method too. This can help with readability. It can also make it easier to use the same check on multiple filters.
before_filter :login_required, except: :index, unless: :js_request?
private
def js_request?
request.format.js?
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With