Rails how to provide file download not through controller? - ruby-on-rails

I want to make a list of file download links to files in my local folder
when I use this
download_link
It complains
No route matches [GET] "/home/tmpfile.txt"
and
<%=link_to "download_link","/home/tmpfile.txt"%>
also get
No route matches [GET] "/home/tmpfile.txt"
add get "/home/tmpfile.txt" to config/route.rb does not solve the problem
I dont want to use the send_file function in controller because I have a lot of files
I am looking for something like this
ftp://ftp.ncbi.nih.gov/pub/mmdb/cdd/
Does anyone have a simple solution?

Is /home/tmpfile.txt the path in your filesystem or the path in the servers virtual filesystem?
If its the path in the filesystem you need to move it to the public folder though its available in the webservers root directory!

Related

Show image with Rails server URL

I have an image in my rails source in rails_project/public/images/2019-12/image1.png
I want to show image by acessing URL like:
http://10.0.5.140:8888/applications/images/2019-12/image1.png
(10.0.5.140 is my IP and 8888 is my rails port)
I tried some solution but I always received the error:
Routing Error
No route matches [GET] "/applications/images/2019-12/image1.png"
Please give me some ideas to resolve my problem. Thanks!
Use asset helper method image_tag(). You should surely read official guide's Coding Links to Assets.
<%= image_tag('/images/2019-12/image1.png') %>
should work for you.
Rails use asset pipeline and serve files from asset load paths (default to app/assets, lib/assets, vendor/assets)
In development files under those load path are served by app server.
In production files under those load path are pre-compiled into /public/assets and served by web server such as nginx.
The image_tag method with
relative file path e.g. 'file.png' searches for /public/assets/file.png
absolute file path e.g. '/images/file.png' searches for /public/images/file.png
Since your image is in public folder. please try this
http://10.0.5.140:8888/images/2019-12/image1.png
To show static images of public folder you can also try this:
<img src="/images/2019-12/image1.png">

Rails 3 redirect a public file to an external path

Using Rails 3 and Ruby 1.9.3, I'm trying to redirect a file in my public folder to an external URL.
This is because this file is going to be managed by another party.
I'm unsure how to accomplish this.
But I need for a file like this:
mysite.com/publicfile.html
to redirect to:
someoneelsessite.com/publicfile.html
Add a route:
get 'publicfile.html', to: redirect('https://someonelsesste.com/publicfile.html')
https://guides.rubyonrails.org/routing.html#redirection
Make sure you DON'T have that publicfile.html file on your public folder, or it will be served before reaching the router.

How to give image path in ruby on rails

I want to use an image that is located in app/assets/revolution/assets/01.png.
I'm not using the by default app/assets/images folder. I want to use my own folder name 'revolution' where I'v made my own assets folder.
How do I give the image path in my index view? I'm trying to do it like this:
<%= image_tag ('revolution/assets/01.png') %>
You need to put that revolution folder in assets path first then you will be able access files from it.
config.assets.paths << Rails.root.join("app/assets", 'revolution')
Than only you will be able to access file from assets path like below.
<%= image_tag('01.png') %>
If you do not want to put your folder in assets images path. We have 2 ways of doing same. Either move your folder to assets/images or to public path (rails ways) in both way you can easily access file.
I think what you need is the helper asset_url
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html#method-i-asset_url
But you would strongly advise you to use the default folder if you want to do that.
#mudasobwa While it's true, you don't have to be such a d..k in your reply :/. It's not as if we never coded shit when discovering new languages / libs / concepts
#Gagan Gami
You could, but you would then avoid the asset pipeline and are going to have a huge cache problem with your browser the day you replace your file while keeping the name.

image_path generating different paths

I am using the image_path helper to include apple-touch-icons. The icons are in the folder app_icons. Here is the folder structure:
Now, the url generated is images/app_icons/icon#2x.png and I get no route matches exception. But for all other images in other folders I get assets/event_logos/xxxx.jpg and it works. I've been using this for a really long while and I am not sure whats going wrong here.
Here is the ERB:
And here is the HTML output:
Rails will fall back to a default path of /images if it cannot find an asset with the specified name in /assets, presumably assuming you will want to render an image from /public/images instead.
Check the filename for typos - make sure you have a file named icon#2x.png in your /app/assets/images/ folder.

Grails application root path

Is there a variable where I can find out the root directory of my Grails application?
for example, I have a folder named chart under \%root%\web-app\images\ where I put my charts in. Once I deploy my Grails application on Jetty, I will get FileNotFoundException because the root path becomes /srv/www/vhosts/domain-name/jetty-version/
I would like to know if there is a variable that returns the root path (like /srv/www/vhosts/domain-name/jetty-version/webapps/myapp), and there should be because CSS uses relative path just fine.
solved.
request.getSession().getServletContext().getRealPath("/")
this actually gives me the path to where my application puts the images, css, WEB-INF, etc. folders.
System.properties['base.dir']
I know it is an old question, but this could work if you are not in an http request:
ServletContextHolder.servletContext.getRealPath('/')
If you want to establish this is GSPs try this:
${createLink(uri: '/')}

Resources