Images desapears after deploy Rails 3.1.3 on Heroku Cedar - ruby-on-rails

After deploy on heroku cedar, images desapear.
I've a CSS like :
:css
/* */
table.table thead .sorting { background: url('assets/datatables/sort_both.png') no-repeat center right; }
table.table thead .sorting_asc { background: url('assets/datatables/sort_asc.png') no-repeat center right; }
table.table thead .sorting_desc { background: url('assets/datatables/sort_desc.png') no-repeat center right; }
/* */
table.table thead .sorting_asc_disabled { background: url('assets/datatables/sort_asc_disabled.png') no-repeat center right; }
table.table thead .sorting_desc_disabled { background: url('assets/datatables/sort_desc_disabled.png') no-repeat center right; }
and relative png into app/assets/images/datatables/Locally works, but not on Heroku.
I could also use = asset_tag('datatables/icon.png') ..., but how to do it inside CSS ?
I've also tried config.action_dispatch.x_sendfile_header = nil in config/environments/production.rb without success.

In the production environment the assets will have an MD5 thumbprint appended to their URL. It is important that you use the asset path helpers so that the right filename is used.
It appears that you are using Haml, based on the :css filter.
In Haml you can interpolate Ruby into the doucment with #{ ruby }
:css
table.table thead .sorting { background-image: url(#{ asset_path('datatables/sort_both.png')}) }
... and so on.
If you are using Sass/SCSS, you can use the built in asset helpers.
table.table thead .sorting {
background-image: asset-url('datatables/sort_both.png');
}
Its a little more complicated if you are using plain CSS. You'll need to append .erb to the css file. ('assets/stylesheets/file.css.erb')
table.table thead .sorting {
background-image: url(<%= asset_path('datatables/sort_both.png') %>);
}
You should use Sass or SCSS. Its the cleanest and leanest.

I just got it working myself.
The key is putting this line in your application.rb:
config.assets.initialize_on_precompile = false
Are you using the jquery-datatables-rails gem? If not, you should! Put this line in your gemfile:
gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
and run:
bundle install
NOTE: Don't put it in your assets group or it will not work when deploying to heroku (since the assets group is not used in production).
Also, make sure to put this line in your application.rb (sorry to repeat, but its important):
config.assets.initialize_on_precompile = false
Add these to your application.js
//= require dataTables/jquery.dataTables
//= require dataTables/jquery.dataTables.bootstrap
Add this to your application.css:
*= require dataTables/jquery.dataTables.bootstrap
And add this to your js.coffee file for your controller you are using datatables in:
If you are using fluid containers:
#// For fluid containers
$('#dashboard').dataTable({
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap"
});
If you are using fixed width containers:
#// For fixed width containers
$('.datatable').dataTable({
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
"sPaginationType": "bootstrap"
});

Related

Media query with rails bootstraps-saas

I m trying to make media queries with bootstraps. I put this in my application.css.scss file but that doesn't work :
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require 'masonry/transitions'
*= require_tree
*= require_self
*/
#import "bootstrap-sprockets";
#import "bootstrap";
#import "font-awesome";
$backgroundNav : #ffffff;
$backgroundBody : #E9E9E9;
$red : #FD0E48;
#media screen and (max-width: 770px){
.body {
background-color: red;
}
}
body {
background: $backgroundBody;
}
What should i do in order to make the query to be detected ?
Application.css.scss is a manifest file and you should define which files you need to require.
If you want to compile codes in your application.css.scss you should add = require_self in top of your application.css.scss
I believe you have the wrong selector.
.body {
background-color: red;
}
.body selects an elements with the class body, like <div class="body">foobar</div>
You probably meant to select the body element:
body {
background-color: red;
}
Also, put your #media styles after the "normal" ones.
body {
background: $backgroundBody;
}
/* responsive body styles */
#media screen and (max-width: 770px){
body {
background-color: red;
}
}
Here's the fiddle - resize the window to see the change.
And since RoR has SASS support, why not use it?
body {
background: $backgroundBody;
#media screen and (max-width: 770px){
background: $backgroundBody;
}
}
First of all your CSS in the media query is wrong.
It should be:
body {
background-color: red;
}
and not .body as it will search for element that has class "body", example:
<p class="body>
content
</p>
Secondly try to write your media queries (#media styles)after your general css towards the end of the files, i think that should fix the issue.
Hope that helps :)

Rails 4 not showing imgaes in Production mode

I read through every link on Stack Overflow and Google. I have spent many hours trying stuff. But could not figure this issue out.
I have following folder structure in my app assets
app
->images
->application (folder)
image1..
image2..
->icons (folder)
image3..
image4..
I have application.css file in my stylesheets folder like below:
app
->stylesheets (folder)
application.css
->application (folder)
app.css
The application.css has following entry:
*= require_tree ./application
Thus it is loading my app.css file which has all the css for my app. In my app.css I am using :
{background: url('icons/user-business-gray.png') no-repeat 2px center transparent;}
My config/environments/production.rb has :
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
#config.serve_static_assets = false
config.assets.compile = false
config.assets.digest = true
My config/initializers/assets.rb has :
Rails.application.config.assets.paths << Rails.root.join( "app/assets/images/application")
I did :
RAILS_ENV=production rake assets:precompile
I can see all my images hashed in public/assets, public/assets/application and public/assets/icons properly.
It all works fine in development mode. But I cannot see any images referenced in in app.css file in production mode.
If I add an image to .erb directly using <%= image_tag('icons/user-business-gray.png') %> I can see the image correctly. But I cannot see any images referenced from app.css as referenced above.
I even changed the name of app.css to app.css.scss and referenced the image like :
{background: url(assets-path('icons/user-business-gray.png')) no-repeat 2px center transparent;}
but still the same issue. Any help is much appreciated.
Try changing the filename to app.css.scss and using the assert-url helper:
{background: asset-url('icons/user-business-gray.png') no-repeat 2px center transparent;}
This has fixed the issue:
{background: image-url('icons/user-business-gray.png') no-repeat 2px center transparent;}

Rails assets pipeline images with relative path arent loading

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.

Rails 3.2.5 Stylesheet Not Being Applied - Newbie

I am learning Rails from "Agile Web Development With Rails" (4th edition, Rails 3) and I am having trouble getting a stylesheet to apply. To quickly take you through what I have done so far, I first generated a scaffold:
rails generate scaffold Product title:string description:text image_url:string price:decimal
Then of course:
rake db:migrate
I then used the db/seeds.rb file to load some data into the products table. When I loaded localhost:3000/products, everything was working great. Now, I copied the stylesheet code the book provides to the products.css.scss file generated by the scaffold:
.products {
table {
border-collapse: collapse;
}
table tr td {
padding: 5px;
vertical-align: top;
}
.list_image {
width: 60px;
height: 70px;
}
.list_description {
width: 60%;
dl {
margin: 0;
}
dt {
color: #244;
font-weight: bold;
font-size: larger;
}
dd {
margin: 0;
}
}
.list_actions {
font-size: x-small;
text-align: right;
padding-left: 1em;
}
.list_line_even {
background: #e0f8f8;
}
.list_line_odd {
background: #f8b0f8;
}
}
Now, when I load localhost:3000/products, the stylesheet is not being applied.I can see the stylesheet in inspect element, but it is not applying. I have tried everything imaginable. Firstly, I made sure this line of code was in my application.html.erb:
<%= stylesheet_link_tag "application", :media => "all" %>
Given that my new stylesheet (products.css.scss) is in the assets/stylesheets/ directory, it should be loaded. When I inspect element on localhost:3000/products, I actually see the stylesheet there, but it is not being applied. the next thing I tried was running:
rake assets:precompile
No success. After deleting the public/assets directory, I made sure this line of code was in my config/application.rb file:
config.assets.enabled = true
It was. Next, I headed over to my assets/stylesheet/application.css file and tried adding this line of code:
*= require products
Once again, no success. Lastly, I tried restarting my server to see if there was an effect. Failure again. I am at dead end here. Please, I would appreciate any and all input on this matter as I am so burnt-out and frustrated with this matter.
i just spent an hour figuring out the answer, since been having the exact same issue myself. go to your application.css.sass and make sure it has
/* ...
*= require_self
*= require_tree .
*/
in it. this auto loads all the other .css.sass in apps/assets/stylesheets, then precompiles them into public/assets/stylesheets to be 1 statis css file, and that is being served to your browser.
not sure if this will work but worth a shot, try removing .css, just keep products.scss. Also get a very simple style going and see if it works. Or even inline it to see.

Rails 3.1 Asset Pipeline and Caching

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)

Resources