I've just started to work with the rails asset pipeline, I'm a front-end guy.
I'm trying to use image-url helper within sass files so that I don't have to hard-code the path.
The following SASS
.some-class
background: image-url("image.png")
generates the following CSS
.some-class{
background: url("asset/image.png");
}
How do I use the image-url helper to generate the following css, without hard-coding the image path ?
.some-class{
background: url("asset/image.png") no-repeat 0 0 #fff;
}
You can use,
.some-class
background-image: image-url("image.png")
background-repeat: no-repeat;
background-color: #fff;
and so on.
or
background: #fff image-url("image.png") no-repeat 0 0;
should work.
You propably should use:
background-image: asset-url("image.png", image) no-repeat 0 0 #fff;
The asset-url is a sass/rails feature, which lets the asset pipeline do some (production) work for you. You can read about that feature here.
Using the helper is preferable over a plain url("image.png"), as it gives you finger printed URLs in production. See this StackOverflow answer.
Related
In a Rails 6 app, I have an image file in app/assets/images/bgs/abc.jpg
To attach that image to body background, how can I have a style declaration in a style tag in application.html.haml that works the same for both local development and when pushed to heroku?
Works in development, but not Heroku, due I suppose to asset handling differences:
#application.html.haml
%style
body::after {
content: "";
background: url("/assets/bgs/abc.jpg"); ### WORKS DEVT ONLY ###
background-size: cover;
background-repeat: no-repeat;
opacity: 0.12;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
background-attachment: fixed;
z-index: -1;
}
I understand I should be using asset_path but none of these work in development:
background: asset_path("/bgs/abc.jpg");
background: asset_path("bgs/abc.jpg");
background: asset_url("/bgs/abc.jpg");
background: asset_url("bgs/abc.jpg");
There is a spectacular amount of inconsistent information out there, some cases saying use asset-url or asset-path, some cases saying asset_url or asset_path, and even some cases saying use url(~filename.jpg), some say use background: some say background-image.
Is there a simple way to use a background image with the exact same code working both on development and Heroku?
Rails on production mode will precompile assets so that all images on assets folder (and child folders) will be precompiled and saved as image name-fingeprint on public folder, for example: i have a image assets/images/bgs/pragmatic_programmer.jpg, then after run command rake assets:precompile, this image will become /public/assets/bgs/pragmatic_programmer-9aa8a13ac97f205fe891bee7c42c6e711f997186d8975d5a4106ca2fe53ccc61.jpg.
That the cause your app work on development mode but not on production mode since the images path changed, you can access new compiled images by ActionView::Helpers::AssetTagHelper#image_path as below
#application.html.haml
%style
body::after {
content: "";
background: url(= image_path("bgs/abc.jpg"));
...
}
you could test on development mode, just run rake assets:precompile before start server.
The answer is for the style definition to be:
background: url( asset_path("bgs/abc.jpg") );
that works on both development and production.
I've searched through Stack lot of times but still nothing works for me.
Images Ive set in my css.erb file does not show in heroku app.
here is my css file example
.bird-box {
position: relative;
height: 600px;
background: url(<%= image_path 'bird-bg.jpg' %>);
background-size: auto 600px;
background-position: top center;
background-attachment: fixed;
overflow: hidden; }
I've changed producion.rb
config.serve_static_files = true;#ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.compile = true
But that did not help at all.
Also I tried to compile locally than push to heroku, still no result. Can anybody tell me the way out?
Go to development console in your browser and check if images are loaded
Try to precompile assets on heroku using heroku run rake assets:precompile
Some nodes in my graph database have a property which contain a URL to an image. I wanted to use Neo4j browser's style sheet functionality to display that image as the background image of a node.
My .grass file looks like this:
node {
diameter: 40px;
color: #DFE1E3;
border-color: #D4D6D7;
border-width: 2px;
text-color-internal: #000000;
caption: '{name}';
font-size: 10px;
background-color: #00aaee;
}
node.Actor {
color: #AD62CE;
border-color: #9453B1;
text-color-internal: #FFFFFF;
background-image: url('{image}');
}
After I uploaded the file, the line background-image: url('{image}'); gets converted to background-image: url({image}); (missing apostrophes). No image is shown in the browser.
What am I doing wrong?
You're not doing anything wrong.
The feature is not supported in the Neo4j browser. Other visualization solutions do exist. Unfortunately the ones that use this feature out of the box are not open source and may require a commercial license.
Of these solutions are:
http://linkurio.us/
https://www.tomsawyer.com/products/visualization/index.php
http://www.keylines.com/
As #Kenny Bastani said most of the libraries that implement this feature have a commercial license.
For a full list of the supported ones have a look to the official Neo4J visualize page.
I'm confused about how works assets pipeline, im using a gem called aloha-rails and it have the next CSS rule:
button.aloha-button {
background: url("../img/base.png") no-repeat scroll 0 0 transparent !important;
}
If I run it on local, that rule is on file:
http://local.dev/assets/aloha/css/aloha.css?body=1
And I CAN see the image, it is loaded from this address:
http://local.dev/assets/aloha/img/base.png
But when I run it on heroku, on when I precompile assets on local the css rule is loaded from applications.css, the rule still with ../img/base.png so the image is trying to be loaded from:
http://server.herokuapp.com/img/base.png
and that DOESNT exist, so I receive a 404 error and dont see the image.
Write as following: Remove the dots(..) before the path.
button.aloha-button {
background: url("/img/base.png") no-repeat scroll 0 0 transparent !important;
}
Please do let me know if not working still.
In order to access your image assets in production, where asset precompilation will stick a hash at the end of each filename, you need to use the img-url helper provided by sass-rails if your css has a .scss or .sass extension, or you can use the asset_path helper if you add .erb to the end of your stylesheet filenames:
button.aloha-button {
background: image-url("/img/base.png") no-repeat scroll 0 0 transparent !important;
}
It seems like the aloha-rails gem doesn't do this. Probably also needs to drop the .. in front of the path.
I'm working with Rails 3.1's asset pipeline and although it seems to work flawlessly in my development environment on my localhost, I'm having huge issues with it on engine yard.
Here's my basic problem.
When I include images for a background in one of my scss files:
a {
color: #3c7f8b;
font-weight: bold;
padding-left: 35px;
font-size: 13px;
display: block;
background: white url(shade.png) top right;
&:hover {
color: #222222;
background: white url(shade2.png) top right; }
&.on {
color: #222222;
background: white url(shade2.png) top right; } } }
I run into the following issues: Even though I have precompiled my assets, the browser requests /assets/shade.png instead of /assets/shade-FINGERPRINT.png which is the actual file that exists.
Does anybody know what I can fix this issues with referencing images in my assets folder inside my .scss files?
You should use image-url rather than url to reference images when using scss in Rails 3.1.
Also, ensure you load compass before sass-rails, as sass-rails over-rides the asset methods to work with the asset pipeline.
Lastly, if you're using capistrano to deploy, add in
load 'deploy/assets'
To enable asset compilation when you deploy.
Check out the answer from 'tybro0103' on this post - Rails 3.1 and Image Assets
basically change the file from scss to scss.erb and then use the asset_path helper method
pre-compile before deploy
disclaimer: i have not tried this myself
qnm Actually I think there is an error with the image_url helper. Not sure if they fixed it but I saw a recommendation to use asset_url with the "image" explicting stated.
i.e. asset_url("some.img","image)