Show image with Rails server URL - ruby-on-rails

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">

Related

Rails - How to get file path (not url path) for assets?

I have a weekly generator that zips some files. Some are assets in app/assets/images/xxx.xxx some are uploaded via Paperclip and in public/system/env...
The file paths I have start after app, without /images/ or public.
I'll get something like /assets/some.png or /system/production/xyz/some.jpg, and these will be served fine as a URL path, but I want to collect them in a file.
How can I get the filepath of an asset?
I think this is an easier way to explain it.
Given the asset URI string, how can I find and get the asset's file path?
The asset URL path /assets/logo.png could be one of the files:
public/logo.png
public/assets/logo.png
app/assets/images/logo.png
In production, assets should be precompile and served from the public root folder /public/assets/logo.png

Rails.application.assets.find_assets(file).digest_path doesn't work in production

I need to iterate through multiple images via a JS file, so I am making an http request to the server to match the filename to its client-side fingerprinted equivalent. I'm getting the fingerprinted file by passing the original file through Rails.application.assets.find_asset(file).digest_path
Example, in the rails console:
Rails.application.assets.find_asset("scene1.jpg").digest_path
returns
"scene1-b691b411ad644bcf2c84ef9e30f52db9ffdf57c18fadf99872dff3ebb81fa548.jpg"
However, when I run on my local server the output is simply nil.
Using the Rails asset pipeline, assets are referenced with their logical path. In this case, you can use embedded ruby (erb) in your script by adding the .erb extension to the file, e.g.
var logo = <%= asset_path('logo.png') %>;
When this script is compiled, the ruby code will produce a reference to the asset with the current digest. The digest path is a "fingerprint" and will change as often as the assets themselves change.
To learn more about the asset pipeline, check out the ROR guide and the Sprockets documentation.

Rails 4 - Accessing stylesheet in public folder from another domain

I have a css file in my public/assets/stylesheets/example.css directory. How can I access this css file directly from a url?
Ive tried www.app.com/assets/stylesheets/example.css but it doesnt work. I thought the public folder was the default folder in production? Im running rails 4 with passenger in a dedicated server (not heroku).
Please help! Thank you
IF you use assets procompilation you need to append the cacheing hash to the end of the filename (e.g. : application-03ed2f1b0877b3bc13330388bee8d3f0.css ) in addition, even if your project has more than one css, in production they will all end up into the same application-[...].css file. One option, other than updating the url every time you update your css, is to make a symlink to the css:
ln -s /var/www/myapp/app/assets/stylesheets/example.css /var/www/myapp/app/public/assets/external-example.css
EDIT:
also the url would be:
http://example.com/assets/application.css
not
http://example.com/assets/stylesheets/application.css

How to get rails to process a literal file path for a static file?

I have an mp3 in <my root directory>/app/assets/sound. I want to be able to access this file from an html file, with the following:
<audio src="/assets/sound/mysong.mp3">
However the html5 audio player does not show up, and when I try to go to this link directly in my browser, I get an error:
Routing Error
No route matches [GET] "/assets/sound/mysong.mp3"
I've tried many variations on the URL, adding the sound directory to my config.assets.path, and setting config.serve_static_assets = true in my production.rb, and nothing is working. What is the fix for this?
Note that I don't require a high-performance server for this web page, so I don't want to handle static assets through Apache or anything else complicated. I just want a simple method for Rails to find a given file and return it.
One easy way to fix this would be to move the file to public/sound/mysong.mp3 and reference it by src="/sound/mysong.mp3". That should work fine, but does not use the asset pipeline.

Rails how to provide file download not through controller?

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!

Resources