Rails 4 - Accessing stylesheet in public folder from another domain - ruby-on-rails

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

Related

A way to display a list of public assets

(Been looking for this for ages over internet...).
Been working with very old npm packages recently and seems that they cannot be loaded properly within Rails-6 (I know I could create a question by each cases, but I'd like to learn to handle this myself since every time it is for a different reason).
I'd like (as for debugs and in development mode only) to display the list of available all assets, including js, css, images, and anything else available at public level (the client could load). So it should be a set of compiled assets ?
Similarly to http://localhost:3000/rails/mailers like http://localhost:3000/rails/assets ?
Displaying the list of all available assets (prior compilation, also for debug intents) could also be great.
This might do what you want:
Rails.application.assets.each_file do | pathname |
# ...
end
This will enumerate every file of which Sprockets is aware. You can run that at rails c or build a simple controller to dump the list into a view if you prefer. For more information, see https://stackoverflow.com/a/11005361.
If you're not using Sprockets (e.g. a newer Webpacker-based application) then, obviously, you'll need a different solution.
You can run rails assets:reveal and see all the available assets:
$ rails assets:reveal
application.js
application.js.map
application.css
application.scss
...
To view the full path, use rails assets:reveal:full:
$ rails assets:reveal:full
/home/{REDACTED}/app/assets/builds/application.js
/home/{REDACTED}/app/assets/builds/application.js.map
/home/{REDACTED}/app/assets/builds/application.css
/home/{REDACTED}/app/assets/stylesheets/application.scss
...

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

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.

Rails 4 image-path, image-url and asset-url no longer work in SCSS files

Are we supposed to use something else aside from image-url and others in Rails 4? They return different values that don't seem to make sense. If I have logo.png in /app/assets/images/logo.png and I do the following, this is what I get:
image-url("logo.png") -> url("/images/logo.png") #obviously doesn't work
image-path("logo.png") -> "/images/logo.png"
asset-url("logo.png") -> url("/logo.png")
Of course none of these work because they need at least /assets in front.
UPDATE: Actually, I just noticed, how do I access images in Rails 4? I have an image at /app/assets/images/logo.png. But if I go to any of the following URLs, I still don't see my image:
http://localhost:3000/assets/logo.png
http://localhost:3000/assets/images/logo.png
http://localhost:3000/logo.png
http://localhost:3000/images/logo.png
UPDATE 2: The only way I can bring up my logo.png is by moving it into the /app/assets/stylesheets directory and then pulling up:
http://localhost:3000/assets/logo.png
I just had this issue myself.
3 points that will hopefully help:
If you place images in your app/assets/images directory, then you should be able to call the image directly with no prefix in the path. ie. image_url('logo.png')
Depending on where you use the asset will depend on the method. If you are using it as a background-image: property, then your line of code should be background-image: image-url('logo.png'). This works for both less and sass stylesheets. If you are using it inline in the view, then you will need to use the built in image_tag helper in rails to output your image. Once again, no prefixing <%= image_tag 'logo.png' %>
Lastly, if you are precompiling your assets, run rake assets:precompile to generate your assets, or rake assets:precompile RAILS_ENV=production for production, otherwise, your production environment will not have the fingerprinted assets when loading the page.
Also for those commands in point 3 you will need to prefix them with bundle exec if you are running bundler.
Your first formulation, image_url('logo.png'), is correct. If the image is found, it will generate the path /assets/logo.png (plus a hash in production). However, if Rails cannot find the image that you named, it will fall back to /images/logo.png.
The next question is: why isn't Rails finding your image? If you put it in app/assets/images/logo.png, then you should be able to access it by going to http://localhost:3000/assets/logo.png.
If that works, but your CSS isn't updating, you may need to clear the cache. Delete tmp/cache/assets from your project directory and restart the server (webrick, etc.).
If that fails, you can also try just using background-image: url(logo.png); That will cause your CSS to look for files with the same relative path (which in this case is /assets).
I just found out, that by using asset_url helper you solve that problem.
asset_url("backgrounds/pattern.png", image)
I had a similar problem, trying to add a background image with inline css. No need to specify the images folder due to the way asset sync works.
This worked for me:
background-image: url('/assets/image.jpg');
Rails 4.0.0 will look image defined with image-url in same directory structure with your css file.
For example, if your css in assets/stylesheets/main.css.scss, image-url('logo.png') becomes url(/assets/logo.png).
If you move your css file to assets/stylesheets/cpanel/main.css.scss, image-url('logo.png') becomes /assets/cpanel/logo.png.
If you want to use image directly under assets/images directory, you can use asset-url('logo.png')
for stylesheets:
url(asset_path('image.jpg'))
In case anyone arrives looking for how to generate a relative path from the rails console
ActionView::Helpers::AssetTagHelper
image_path('my_image.png')
=> "/images/my_image.png"
Or the controller
include ActionView::Helpers::AssetTagHelper
image_path('my_image.png')
=> "/images/my_image.png"

Access heroku environment variable in coffeescript file

I have a scenario where I want my CoffeeScript file to access an environment variable like an API key value. This works fine locally but it isn't working when I push it up to heroku.
The file is named something like myfile.js.coffee.erb
I am setting the value like this
api_key = '<%= ENV['SERVICE_API_KEY'] %>'
I know the values are set in heroku and I have triple checked the spelling, etc. I know it is being processed since the resulting JavaScript file looks like this
var api_key;
api_key = "";
Is there something I need to do when precompiling my assets where I can tell it to access environment variables? I admit that I am new to CoffeeScript and the Rails asset pipeline. Is there another more accepted way of doing this? I don't want to embed it in the file for obvious reasons.
So since the API key is going to be visible to those interested whether it's in the javascript file or the html file and since you really don't want to be generating a new .js file every request, the easiest solution I've found to the same problem is to put the key in your layout.html.erb file.
You can throw it in a script tag, use a data-attr, whatever floats your boat, but it works and you get the benefit of a dynamic variable and having to render one less file.
And you can still use the <%= ENV['api_key'] %>. You'll have to fetch the variable in your js (or coffee), but that's pretty trivial.
Try this
heroku labs:enable user_env_compile -a myapp
then deploy again
Worked for us!
all credits to this guy:
Heroku always runs assets:precompile with the production environment for Rails 3.2

Resources