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.
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.
Another way, which I think is cleaner than the accepted answer, is to use the
if
orunless
options with a lambda. I just like having the method name listed as a symbol so it's standardized with the other controller filters. This works in Rails 3 and up.