Solved

html link to a file outside rails application directory using ActionView

Posted on 2008-10-18
7
2,805 Views
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

Thanks,
GT.
0
Comment
Question by:gt9
  • 3
  • 2
  • 2
7 Comments
 
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!
0
 

Author Comment

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

Expert Comment

by:Andrew Doades
ID: 22752544
so something like

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

this is the olny way I can think to do it
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 14

Expert Comment

by:wesgarrison
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:
http://api.rubyonrails.com/classes/ActionController/Streaming.html
Specifically, you probably want send_file()

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

Open in new window

0
 

Author Comment

by:gt9
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?

Thanks,
GT.

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

0
 

Author Comment

by:gt9
ID: 22754793
0
 
LVL 14

Accepted Solution

by:
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

end

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now