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

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

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: what is the url to an image hosted in assets?

I have an image hosted on my rails server in the assets folder, I'd like to access this image through an url rather than using the path so that I can display it in my react component.
How can I find the url for said image so that when I go to the url the image will be displayed?
You have to use image_tag. If you have "image.png" in your assets, you can use
<%= image_tag "image.png" %>
and it will result in a URL like this
/assets/image-bb14d53a12ca1aa006defb5c0f0923aa3c946f7e2dfd534846b3a1d3b8aac72e.png
When using image_tag, Rails creates a random token for image authentication. You can't just use <img src="/assets/image.png because there is no token.
Rails' assets pipeline creates a digest of each asset and adds that to the filename, so the name is not always the same, you can't just copy it and use it forever (you can make rails recreate all the digests and all the urls are changed for example).
I can think of few options:
1 - Disable the assets pipeline digest feature
On your environment's config file, add config.assets.digest = false and now the name won't have the digest at the end. Read the docs though, since the digest is there for a reason, you can disable it but it's better to decide that after reading the doc.
https://guides.rubyonrails.org/asset_pipeline.html#turning-digests-off
2 - Move those images to /public
If you move the images to /public, they won't be compiled by the assets pipeline.
3 - Use a gem like non-stupid-digest to select which assets should have it
https://rubygems.org/gems/non-stupid-digest-assets lets you configure some assets to prevent the assets pipeline to skip the digest generation without turning off the feature for all the assets
4 - Let the assets pipeline alone and put the urls somewhere on your html
To get the correct current path of an image, you can use the helper image_path (for example: image_path('something.png') returns /assets/something-123...890.png). You can then write that on your template like:
<script>
imageUrl = '<%= image_url('something.png') %>';
</script>
And you'll have access to that imageUrl variable on your javascript code.
As far as I know, you will need to go into your public folder and then to assets.
Find the image you want and then copy it's name.
You will need to call it like: /assets/img-name-033cce876b38fb47072fb3668a5.jpg or /assets/folder/ before the image name if it's in another folder and so on.

Rails 4 - how do I get the images path of an image in production with a hash on the end?

I want to reference an image in the public folder that has been precompiled in prod. But, it seems that all the images have a hash on the end. (i.e., assets/image-3414fewafe313.jpg)
asset_path(photo) = assets/photo.jpg (i need the full image path with hash)
How do I reference this image in a view in Rails? Thanks!
For a view, you can just reference image_path('photo.jpg')
http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets
See also image_tag('photo.jpg') which produces a full HTML img tag.
The hash at the end of the asset's URL is the result of having the config.assets.digest parameter set to true
Quoting from http://edgeguides.rubyonrails.org/asset_pipeline.html#in-production:
In the production environment Sprockets uses the fingerprinting scheme outlined above. By default Rails assumes assets have been precompiled and will be served as static assets by your web server.
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disk. These fingerprinted names are used by the Rails helpers in place of the manifest 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.

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.

Resources