views:

374

answers:

1

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?

+5  A: 

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
jdl
Thank you for your help. I only had to replace the login_required with controller.send(:login_required) before_filter(:only => :index) do |controller| controller.send(:login_required) unless controller.request.format.js? end
gdelfino
Let me repeat my comment as the code didn't show up as I was expecting.I only had to replace the login_required with controller.send(:login_required) Thanks.
gdelfino