• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2975
  • Last Modified:

html link to a file outside rails application directory using ActionView

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
gt9
Asked:
gt9
  • 3
  • 2
  • 2
1 Solution
 
Andrew DoadesIT TechnicianCommented:
something like this?

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

this looks to a folder called tmp under the public folder!
0
 
gt9Author Commented:
I would like to refer to a file location outside my rails project.
0
 
Andrew DoadesIT TechnicianCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
wesgarrisonCommented:
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
 
gt9Author Commented:
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
 
gt9Author Commented:
0
 
wesgarrisonCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now