# Adding a before\_filter to static content

**URL:** <https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506>\
**Category:** Rails\
**Created:** [June 29, 2006, 4:25am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506 "2006-06-29T04:25:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 29, 2006, 4:25am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506/1 "2006-06-29T04:25:33Z")

</div>

Hi,

I have a Rails site that uses a before\_filter in the application  
controller that does some authentication and authorization work.

I also have a bunch of HTML in folders in the application’s public  
directory.

Before a user can view anything in the public directory, I’d like to  
authenticate them. How could I do that?

I’m using mongrel, if it matters.

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 29, 2006, 4:53am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506/2 "2006-06-29T04:53:49Z")

</div>

On Wed, 2006-06-28 at 19:24 -0700, Joe Van D. wrote:

> I’m using mongrel, if it matters.  
> You could probably cook up a little Mongrel filter to do this if you  
> were feeling adventurous. Go check out the source to  
> lib/mongrel/handlers.rb for examples, and read the RDoc on the mongrel  
> documentation site. Feel free to e-mail me off list if you hit  
> problems.

–  
Zed A. Shaw

> **[Zed A. shaw](https://zedshaw.com/)**
>
> Personal essays and writings by Zed A. Shaw.

[http://mongrel.rubyforge.org/](http://mongrel.rubyforge.org/)  
[http://www.railsmachine.com/](http://www.railsmachine.com/) – Need Mongrel support?

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 29, 2006, 5:07am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506/3 "2006-06-29T05:07:28Z")

</div>

On 6/28/06, Zed S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’m using mongrel, if it matters.
> 
> You could probably cook up a little Mongrel filter to do this if you  
> were feeling adventurous. Go check out the source to  
> lib/mongrel/handlers.rb for examples, and read the RDoc on the mongrel  
> documentation site. Feel free to e-mail me off list if you hit  
> problems.

Thanks – I’ll check it out. Should it be straightforward?

Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 29, 2006, 6:09am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506/4 "2006-06-29T06:09:26Z")

</div>

On 6/28/06, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m using mongrel, if it matters.  
> Hm…

Say my public directory looks like:

/public  
/docs  
index.html  
page\_1.html

What if I create a Rails controller named docs. It would have one  
action, that would check to see if the requested file existed. If it  
did, then it would render the requested file that’s in the public/docs  
directory.

Does that make sense? Anyone think it’ll work?

Joe

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 29, 2006, 5:57am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506/5 "2006-06-29T05:57:09Z")

</div>

On Wed, 2006-06-28 at 20:04 -0700, Joe Van D. wrote:

> On 6/28/06, Zed S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On Wed, 2006-06-28 at 19:24 -0700, Joe Van D. wrote:

> > You could probably cook up a little Mongrel filter to do this if you  
> > were feeling adventurous. Go check out the source to  
> > lib/mongrel/handlers.rb for examples, and read the RDoc on the mongrel  
> > documentation site. Feel free to e-mail me off list if you hit  
> > problems.
> 
> Thanks – I’ll check it out. Should it be straightforward?

Yes, but not easy like Rails. I’m thinking maybe 2 hours work if you  
want to hook up basic auth. More if you get fancy and need database  
access and such, but you could probably poach that from rails.

–  
Zed A. Shaw

> **[Zed A. shaw](https://zedshaw.com/)**
>
> Personal essays and writings by Zed A. Shaw.

[http://mongrel.rubyforge.org/](http://mongrel.rubyforge.org/)  
[http://www.railsmachine.com/](http://www.railsmachine.com/) – Need Mongrel support?

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 29, 2006, 8:14am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506/6 "2006-06-29T08:14:41Z")

</div>

On 6/28/06, Senthil N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > page_1.html
> > 
> > ```
> 
> It does not make any sense to add authentication for public folders
> 
> maybe I would suggest keep your html out of public and when a url is  
> called, fetch your file and send, it would be through some controller,  
> where you can have your before\_filter or something.

Yeah, that would probably be best. I’ll try it.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [June 29, 2006, 7:57am UTC](https://www.ruby-forum.com/t/adding-a-before-filter-to-static-content/96506/7 "2006-06-29T07:57:59Z")

</div>

Joe Van D. wrote:

> On 6/28/06, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’m using mongrel, if it matters.  
> > Hm…
> 
> Say my public directory looks like:
> 
> /public  
> /docs  
> index.html  
> page\_1.html
> 
> What if I create a Rails controller named docs. It would have one  
> action, that would check to see if the requested file existed. If it  
> did, then it would render the requested file that’s in the public/docs  
> directory.
> 
> Does that make sense? Anyone think it’ll work?
> 
> Joe

It does not make any sense to add authentication for public folders

maybe I would suggest keep your html out of public and when a url is  
called, fetch your file and send, it would be through some controller,  
where you can have your before\_filter or something.

regards  
A.Senthil N.  
[http://senthilnayagam.com](http://senthilnayagam.com)
