html link to a file outside rails application directory using ActionView

Posted on 2008-10-18
Last Modified: 2013-11-19
How to create a html link to a file outside rails application directory using ActionView?
I am trying to render a file stored in my /tmp directory using render or link_to methods of action view. I would like to open this link in a new browser tab or window.
When I use render or send_file method from controller then it does point to file in/tmp directory. However, this does not work from view. The rails looks for this file within view dir. How can I override this/ specify absolute path?
Missing template /tmp/test.html in view path /home/gt9/NetBeansProjects/convfile/app/views

Question by:gt9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 10

Expert Comment

by:Andrew Doades
ID: 22752174
something like this?

<%= link_to "test", "/tmp/test.html" %>

this looks to a folder called tmp under the public folder!

Author Comment

ID: 22752432
I would like to refer to a file location outside my rails project.
LVL 10

Expert Comment

by:Andrew Doades
ID: 22752544
so something like

<%= link_to "test", "file:///C:/tmp/test.htm" l%>
<%= link_to "test", "http://localhost/test.html">%>

this is the olny way I can think to do it
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

LVL 14

Expert Comment

ID: 22752899
You can't link directly to a file outside of the /public directory. That's the only directory served by the webserver!

However, you can read the file in and send it as a stream to the browser.  I use this technique for creating a pdf and then sending it to the browser.

Your link will actually be to an action that grabs the file and sends it.  Don't pass the path and filename into the action or someone could grab any file they want off your system!

Check out the API for the ActionController::Streaming module:
Specifically, you probably want send_file()

send_file '/path/to.jpeg', :type => 'image/jpeg'

Open in new window


Author Comment

ID: 22753784
I am trying to develop a similar feature, although I am generating text and html content from MS Word files. I can render these files using render and send_file methods. However, I would like these files to be rendered in a new tab or a new window. I did not see any options for doing this with ActionController methods. The link_to method offers this option to me.

Is there any way I can open a new tab using render or send_file methods?


render :file => "/tmp/test.html", :layout => false
# another method
send_file '/tmp/test.html', :type => 'text/html; charset=utf-8', :disposition => 'inline'

Open in new window


Author Comment

ID: 22754793
LVL 14

Accepted Solution

wesgarrison earned 125 total points
ID: 22755279
Use link_to to make a link to the action that returns the file.
# template:
<%= link_to "View file in new tab", {:action => 'show_file'}, {:target => '_new'} %>
# controller
def show_file
  render :file => "/tmp/test.html", :layout => false

Open in new window


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google Analytics - viewing a page 10 83
Read text on Table 7 46
ReadyAPI 1.9.0 2 42
Inserting HTML Code on Wordpress Homepage for Affiliate Marketing Tracking 3 18
Find out what you should include to make the best professional email signature for your organization.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question