# Simple Link to Static File

**URL:** <https://www.ruby-forum.com/t/simple-link-to-static-file/78051>\
**Category:** Rails\
**Created:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/simple-link-to-static-file/78051 "2006-12-03T00:06:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/simple-link-to-static-file/78051/1 "2006-12-03T00:06:54Z")

</div>

I have an application in which I create a file in the controller, and  
then in the view I want to be able to provide a link to that file for  
the user. (the file could be a .doc, or .pdf, say)

In the controller I do something like this:

@foo = ‘foo.doc’  
foofile = File.new(@foo, ‘w’)  
… write info to file, and then close it

In the view, I have tried every which way to provide a link ‘foo.doc’  
but without success.

For example, if I just put in the HTML for the file it gets resolved  
something like “localhost:3000/foo.doc” and then I get an error because  
that isn’t an action.

many thanks in advance for any help you can give.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 3, 2006, 4:03am UTC](https://www.ruby-forum.com/t/simple-link-to-static-file/78051/2 "2006-12-03T04:03:48Z")

</div>

> I have an application in which I create a file in the controller, and  
> then in the view I want to be able to provide a link to that file for  
> the user. (the file could be a .doc, or .pdf, say)
> 
> In the controller I do something like this:
> 
> @foo = ‘foo.doc’  
> foofile = File.new(@foo, ‘w’)  
> … write info to file, and then close it

Hrmm… not sure where “foo.doc” is going to get written out to since  
that’s relative to something, but odds are it’s probably not what you  
want…

foo = File.join(RAILS\_ROOT, ‘public’, ‘foo.doc’)  
foofile = File.new(foo, ‘w’)  
… write into file and close it…

In your view…

\<%= link\_to “my file is here”, “/foo.doc” %\>

Not that I would recommend you put all your files into public, but the  
above should work… at the very least put them into a sub directory  
(ie.  
/public/files or something).

-philip
