Using Rails 3.2.
I have half a dozen controllers, and want to protect some (but not all) of them with http_basic_authenticate_with
.
I don't want to manually add http_basic_authenticate_with
to each controller (I could add another controller in the future and forget to protect it!). It seems the answer is to put it in application_controller.rb
with an :except
arg which would list the controllers that should not be protected. The problem is, the :except clause wants method names rather than external controller module names, e.g.:
http_basic_authenticate_with :name => 'xxx', :password => 'yyy', :except => :foo, :bar
So then I thought "Wait, since I already have the protected controllers grouped in routes.rb
, let's put it there." So I tried this in my routes:
scope "/billing" do
http_basic_authenticate_with :name ...
resources :foo, :bar ...
end
But now I get
undefined method `http_basic_authenticate_with'
What's the best way to approach this?
Do it the way Rails does it.
All that
http_basic_authenticate_with
does is add abefore_action
. You can just as easily do the same yourself:which means you can use
skip_before_action
in controllers where this behavior isn't desired: