Topic: before_filter and respond_to

Hi,

I need to prevent access to my whole app except for some actions that respond to a particular format (e.g. xml)

So in my ApplicationController I have (BTW authlogic used here)

before_filter :require_user

To accomplish this task i came up with this solution inside my controller:

  
  before_filter :require_user, :except => [:index]

  before_filter(:only => :index) do |controller|
    controller.send(:require_user)  unless controller.request.format.xml?
  end

repeating this snippet of code for all the action's respond_to formats I need to allow...

how can I DRY out the code?

thanks

Teo

Last edited by tejo (2009-10-20 03:41:04)

Re: before_filter and respond_to

That's going to get ugly really fast if you need to do this in a lot of places... I like to use Acl9 for access control, but I don't think it would be much better than what you already cooked up.

Any chance you can just extract into a separate namespace that only has open controllers that respond to the index.xml requests? For example, if you're creating an API, you might have an api controller namespace. Just a thought.

I'm a Phoenix Ruby on Rails developer and startup junkie kicking ass at Flatterline, a Ruby on Rails web application development company.

Re: before_filter and respond_to

curtis wrote:

Any chance you can just extract into a separate namespace that only has open controllers that respond to the index.xml requests? For example, if you're creating an API, you might have an api controller namespace. Just a thought.

Yes, I think this is the best solution.

Thanks