I want the /admin route on my rails app to be protected by using .htaccess password files – is this possible?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Rails has a built-in helper for this, you could place this in your application controller:
Then use a before_filter on any controllers you want to protect (or just stick it in the application controller to block the whole site):
This method works on Nginx as well as Apache, which is an added bonus. It doesn’t, however, work if you have full page caching enabled – as the visitor never hits the Rails stack; it won’t kick in.
Edit
Just noticed that you specified the /admin route. All my admin controllers inherit from an AdminController. You could set yours up like so:
/app/controllers/admin/admin_controller.rb
Then have all your controllers extend the admin controller, eg:
My routes are setup like so:
Hope that helps.