# How would I set up a Rails-style Public dir in a Gem?

**URL:** <https://www.ruby-forum.com/t/how-would-i-set-up-a-rails-style-public-dir-in-a-gem/195058>\
**Category:** Rails\
**Created:** [September 16, 2010, 1:37am UTC](https://www.ruby-forum.com/t/how-would-i-set-up-a-rails-style-public-dir-in-a-gem/195058 "2010-09-16T01:37:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [September 16, 2010, 1:37am UTC](https://www.ruby-forum.com/t/how-would-i-set-up-a-rails-style-public-dir-in-a-gem/195058/1 "2010-09-16T01:37:11Z")

</div>

I am working on a Rails CMS gem. I want the Gem to have its own Public  
dir (like Rails) to hold css/js/images etc… I have been trying a lot  
of different pathing options from my Gem-centric views, but no matter  
what I try I can’t target any of the files.

Any thoughts on how to set this up?

Thanks, Elliott G

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [September 16, 2010, 1:48am UTC](https://www.ruby-forum.com/t/how-would-i-set-up-a-rails-style-public-dir-in-a-gem/195058/2 "2010-09-16T01:48:53Z")

</div>

> I am working on a Rails CMS gem. I want the Gem to have its own Public  
> dir (like Rails) to hold css/js/images etc… I have been trying a lot  
> of different pathing options from my Gem-centric views, but no matter  
> what I try I can’t target any of the files.
> 
> Any thoughts on how to set this up?

I don’t recall the plugin/gem that did this, but it ran all of the  
css/js/images through Rails routes with caching. So you get penalized  
that first time, but from then on they are cached…

Or you might override compute\_path() and have it look in your gem/public  
directory first or last if it doesn’t already exist, etc.

-philip

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [September 16, 2010, 1:59am UTC](https://www.ruby-forum.com/t/how-would-i-set-up-a-rails-style-public-dir-in-a-gem/195058/3 "2010-09-16T01:59:25Z")

</div>

On Thursday 16 September 2010, elliottg wrote:

> I am working on a Rails CMS gem. I want the Gem to have its own  
> Public dir (like Rails) to hold css/js/images etc… I have been  
> trying a lot of different pathing options from my Gem-centric views,  
> but no matter what I try I can’t target any of the files.

I don’t think you can do what you’re trying to do in Rails alone,  
however, I’d appreciate to be shown wrong. The reason is that public is  
not handled by your application at all. Rather, requests to files in  
public are handled by the web server – and that’s a good thing, because  
apache or nginx are much faster at serving static files than Rails  
proper would be.

I take it, the usual way to add assets from a gem is to include a  
generator that copies (or links) these assets to the application’s  
public directory.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [September 16, 2010, 3:18am UTC](https://www.ruby-forum.com/t/how-would-i-set-up-a-rails-style-public-dir-in-a-gem/195058/4 "2010-09-16T03:18:25Z")

</div>

Thanks for the helpful input guys!

I may end up just using a generator to move a dir to Rails public.  
That would work, I just prefer the idea of the Gem encapsulating  
everything so that when a user updates the Gem they will have all the  
new assets without having to run an “update” task or generator.

Elliott G

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [September 16, 2010, 2:00am UTC](https://www.ruby-forum.com/t/how-would-i-set-up-a-rails-style-public-dir-in-a-gem/195058/5 "2010-09-16T02:00:23Z")

</div>

Elliott G. wrote:

> I am working on a Rails CMS gem. I want the Gem to have its own Public  
> dir (like Rails) to hold css/js/images etc… I have been trying a lot  
> of different pathing options from my Gem-centric views, but no matter  
> what I try I can’t target any of the files.
> 
> Any thoughts on how to set this up?

ln -s mygem/public RAILS\_ROOT/public/mygem

I’m not convinced that this is a great idea.

> Thanks, Elliott G

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

Sent from my iPhone
