Image not showing in production - ruby-on-rails

I have the following code in my view:
<p>
<%= image_tag( "folder/"+"image_name.jpeg" ) %>
</p>
In development mode when I visit the server the image shows and its path is:
http://localhost:3000/assets/folder/image_name-f562b410b0627f8099a44de48f5ee8fff6b1babf98dacfcf307a39a8b7fefbca.jpg
In production mode when I visit the page the image does not shows and its path is:
domain/images/folder/image_name.jpeg
the image in the production site is in the following path:
domain/assets/folder/image_name-f562b410b0627f8099a44de48f5ee8fff6b1babf98dacfcf307a39a8b7fefbca.jpg
Why does the link does not update automatically in the production mode like is updated in the development mode.
I already tried:
rake assets:precompile RAILS_ENV=production

(Not Working) Try in following order:
rails assets:clobber
rails assets:precompile
Try:
check whether config.assets.compile is set to true in config/environments/production.rb

I was able to solve this problem by changing:
config.assets.compile = false to
config.assets.compile = true
in /config/environments/production.rb

Related

Rails generate incorrect asset path with set 'config.assets.compile = false'

I generate a new rails application, and run it in production, but the assets path generate incorrect. Please notice the follow picture.
And even though set config.assets.prefix = '/assets' not work too
I am using rails 4.2.4 and there is the demo project on github
You need to execute RAILS_ENV=production rake assets:precompile manually if the config.assets.compile is set to false.

Images broken after uploading Rails app to heroku

Shows fine locally. But when I upload to Heroku, I get the following:
<%= image_tag('logo-red.png') %>
and it's located in assets/images/
I'm not using Turbolinks. Do I need to run a command on Heroku to solve this or is there some config setting I'm missing?
edit 1: tried running heroku run rake assets:precompile RAILS_ENV=production
edit 2: response from heroku staff:
It looks like your app is properly compiling that image:
~/public/assets $ pwd
/app/public/assets
~/public/assets $ ls | grep logo-red
logo-red-a07050d882e1dba431cef2130d39f929c611eaf8b0ec3c50db0742ddccb14d93.png
edit 3: See attached screenshot
Try running on your local computer:
rake assets:precompile
rake assets:clean
Then commit and push to heroku.
Also check your production.rb file and make sure everything related to compiling or precompiling is true and not false ie:
config.serve_static_assets = true
config.assets.compile = true
Also, make sure rails_12factor is in your gemfile like so:
gem 'rails_12factor', group: :production
Had this problem on Heroku before - we solved it by precompiling the assets on Heroku itself:
$ heroku run rake assets:precompile RAILS_ENV=production
I know this is done when you push the repo to Heroku; it's one of those quirks which seems to be resolved if you compile the assets on their server.
You could also precompile locally as long as you make sure the RAILS_ENV is production:
$ rake assets:precompile RAILS_ENV=production
After below answers if your problem still continuing
you ca try:
<%= asset_path 'logo-red.png' %>

Rails: assets unavailable in staging environment

I'm trying to setup a staging environment.
I copied the environments/production.rb to create environments/staging.rb.
I tested the staging environment locally and ran
RAILS_ENV=staging rake assets:clean assets:precompile
RAILS_ENV=staging rails s
Assets are properly generated in public/assets, and when I load a page, the links to the assets are correct (eg: /assets/application-e014056a81279320a97c71ed25eb46ab.css)
But the browser can't load them and if I try to open
http://localhost:3000/assets/application-e014056a81279320a97c71ed25eb46ab.css
I get a 404.
Now the strange thing is that it works in production, with:
RAILS_ENV=production rake assets:clean assets:precompile
RAILS_ENV=production rails s
For both environments/staging.rb and environments/production.rb, the config is
config.serve_static_assets = false
config.assets.compile = false
config.assets.digest = true
And in application.rb
Bundler.require(*Rails.groups(assets: %w(development test)))
Do you have any idea where to look ? What else could differentiate the staging environment from the production environment ?
So, I had forgotten the role of the 'rails_12factor' gem.
I updated my Gemfile to
gem 'rails_12factor', group: [:staging, :production]
And everything works now.

Rails 4: assets not loading in production

I'm trying to put my app into production and image and css asset paths aren't working.
Here's what I'm currently doing:
Image assets live in /app/assets/images/image.jpg
Stylesheets live in /app/assets/stylesheets/style.css
In my layout, I reference the css file like this: <%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
Before restarting unicorn, I run RAILS_ENV=production bundle exec rake assets:precompile and it succeeds and I see the fingerprinted files in the public/assets directory.
When I browse to my site, I get a 404 not found error for mysite.com/stylesheets/styles.css.
What am I doing wrong?
Update:
In my layout, it looks like this:
<%= stylesheet_link_tag "bootstrap.min", media: "all", "data-turbolinks-track" => true %>
<%= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
The generate source is this:
<link data-turbolinks-track="true" href="/stylesheets/bootstrap.min.css" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/stylesheets/styles.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-0c647c942c6eff10ad92f1f2b0c64efe.js"></script>
Looks like Rails is not properly looking for the compiled css files. But it's very confusing why it's working correctly for javascripts (notice the /assets/****.js path).
In rails 4 you need to make the changes below:
config.assets.compile = true
config.assets.precompile = ['*.js', '*.css', '*.css.erb']
This works with me. use following command to pre-compile assets
RAILS_ENV=production bundle exec rake assets:precompile
Best of luck!
I just had the same problem and found this setting in config/environments/production.rb:
# Rails 4:
config.serve_static_assets = false
# Or for Rails 5:
config.public_file_server.enabled = false
Changing it to true got it working. It seems by default Rails expects you to have configured your front-end webserver to handle requests for files out of the public folder instead of proxying them to the Rails app. Perhaps you've done this for your javascript files but not your CSS stylesheets?
(See Rails 5 documentation). As noted in comments, with Rails 5 you may just set the RAILS_SERVE_STATIC_FILES environment variable, since the default setting is config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?.
In /config/environments/production.rb I had to add this:
Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.
It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.
I was able to solve this problem by changing:
config.assets.compile = false to
config.assets.compile = true in /config/environments/production.rb
Update (June 24, 2018): This method creates a security vulnerability if the version of Sprockets you're using is less than 2.12.5, 3.7.2, or 4.0.0.beta8
For Rails 5, you should enable the follow config code:
config.public_file_server.enabled = true
By default, Rails 5 ships with this line of config:
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
Hence, you will need to set the environment variable RAILS_SERVE_STATIC_FILES to true.
There are 2 things you must accomplish to serve the assets in production:
Precompile the assets.
Serve the assets on the server to browser.
1) In order to precompile the assets, you have several choices.
You can run rake assets:precompile on your local machine, commit it to source code control (git), then run the deployment program, for example capistrano. This is not a good way to commit precompiled assets to SCM.
You can write a rake task that run RAILS_ENV=production rake assets:precompile on the target servers each time you deploy your Rails app to production, before you restart the server.
Code in a task for capistrano will look similar to this:
on roles(:app) do
if DEPLOY_ENV == 'production'
execute("cd #{DEPLOY_TO_DIR}/current && RAILS_ENV=production rvm #{ruby_string} do rake assets:precompile")
end
end
2) Now, you have the assets on production servers, you need to serve them to browser.
Again, you have several choices.
Turn on Rails static file serving in config/environments/production.rb
config.serve_static_assets = true # old
or
config.serve_static_files = true # new
Using Rails to serve static files will kill your Rails app performance.
Configure nginx (or Apache) to serve static files.
For example, my nginx that was configured to work with Puma looks like this:
location ~ ^/(assets|images|fonts)/(.*)$ {
alias /var/www/foster_care/current/public/$1/$2;
gzip on;
expires max;
add_header Cache-Control public;
}
Rails 4 no longer generates the non fingerprinted version of the asset: stylesheets/style.css will not be generated for you.
If you use stylesheet_link_tag then the correct link to your stylesheet will be generated
In addition styles.css should be in config.assets.precompile which is the list of things that are precompiled
change your Production.rb file line
config.assets.compile = false
into
config.assets.compile = true
and also add
config.assets.precompile = ['*.js', '*.css', '*.css.erb']
What you SHOULD NOT do:
Some of my colleagues above have recommended you to do this:
config.serve_static_assets = true ## DON”T DO THIS!!
config.public_file_server.enabled = true ## DON”T DO THIS!!
The rails asset pipeline says of the above approach:
This mode uses more memory, performs more poorly than the default and is not recommended. See here: (http://edgeguides.rubyonrails.org/asset_pipeline.html#live-compilation)
What you SHOULD do:
Precompile your assets.
RAILS_ENV=production rake assets:precompile
You can probably do that with a rake task.
I'm running Ubuntu Server 14.04, Ruby 2.2.1 and Rails 4.2.4 I have followed a deploy turorial from DigitalOcean and everything went well but when I go to the browser and enter the IP address of my VPS my app is loaded but without styles and javascript.
The app is running with Unicorn and Nginx. To fix this problem I entered my server using SSH with my user 'deployer' and go to my app path which is '/home/deployer/apps/blog' and run the following command:
RAILS_ENV=production bin/rake assets:precompile
Then I just restart the VPS and that's it!
It works for me!
Hope it could be useful for somebody else!
If precompile is set you DO NOT need
config.assets.compile = true
as this is to serve assets live.
Our problem was we only had development secret key base set in config/secrets.yml
development:
secret_key_base: '83d141eeb181032f4070ae7b1b27d9ff'
Need entry for production environment
The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files (this will include all image assets automatically) from app/assets folders including your gems:
If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb:
Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
First of all check your assets, it might be possible there is some error in pre-compiling of assets.
To pre-compile assets in production ENV run this command:
RAILS_ENV=production rake assets:precompile
If it shows error, remove that first,
In case of "undefined variable" error, load that variable file before using it in another file.
example:
#import "variables";
#import "style";
in application.rb file set sequence of pre-compiliation of assets
example:
config.assets.precompile += [ 'application.js', 'admin.js', 'admin/events.js', 'admin/gallery.js', 'frontendgallery.js']
config.assets.precompile += [ 'application.css', 'admin.css','admin/events.css', 'admin/gallery.css', 'frontendgallery.css']
Found this:
The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role.
in config/environments/production.rb:
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
So set env RAILS_SERVE_STATIC_FILES or using Nginx to serving static files.
Add config.serve_static_assets = true will still work, but removed in future.
it is not recommended to let capistrano do assets precompile, because it may take ages and often time out. try to do local assets precompile.
1st, set in config/application.rb
config.assets.initialize_on_precompile = false
then do local
RAILS_ENV=production bin/rake assets:precompile
and add those public/assets to git.
and config/environments/development.rb, change your asset path to avoid using precompiled assets:
config.assets.prefix = '/dev-assets'
If you have db connection issue, means u have initializer that uses db. one way around it is to set a new environment by duplicate production.rb as maybe production2.rb, and in database.yml, add production2 environment with development db setting. then do
RAILS_ENV=production2 bin/rake assets:precompile
if you are still facing some issue with assets, for example ckeditor,
add the js file into config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( ckeditor.js )
I may be wrong but those who recommend changing
config.assets.compile = true
The comment on this line reads: #Do not fallback to assets pipeline if a precompiled asset is missed.
This suggests that by setting this to true you are not fixing the problem but rather bypassing it and running the pipeline every time. This must surely kill your performance and defeat the purpose of the pipeline?
I had this same error and it was due to the application running in a sub folder that rails didn't know about.
So my css file where in home/subfolder/app/public/.... but rails was looking in home/app/public/...
try either moving your app out of the subfolder or telling rails that it is in a subfolder.
location ~ ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header ETag "";
}
This fixed the problem for me in production. Put it into the nginx config.
Even we faced the same problem where RAILS_ENV=production bundle exec rake assets:precompile succeeded but things did not work as expected.
We found that unicorn was the main culprit here.
Same as your case, even we used to restart unicorn after compiling the assets. It was noticed that when unicorn is restarted, only its worker processes are restarted and not the master process.
This is the main reason the correct assets are not served.
Later, after compiling assets, we stopped and started unicorn so that the unicorn master process is also restarted and the correct assets were getting served.
Stopping and starting unicorn brings around 10 secs on downtime when compared to restarting the unicorn. This is the workaround that can be used where as long term solution is move to puma from unicorn.

Ruby on Rails 3.2.1 doesn't show images on Heroku under Nivo slider

My Ruby on Rails 3.2.1 APP doesn't show pictures when it is uploaded on Heroku and I really don't know where is the problem. On localhost:3000 all is working prefect. I have made a test and added a picture in content, not under Nivo slider tags and all is perfect, but under Nivo my app doesn't displaying anything.
Nivo slider tags:
<div class="slider-wrapper theme-default">
<div class="ribbon"></div>
<div id="slider" class="nivoSlider">
<%=image_tag "/assets/birz.JPG"%>
<%=image_tag "100.jpg"%>
<%=image_tag "102.jpg"%>
<%=image_tag "103.jpg"%>
<%=image_tag "104.jpg"%>
<%=image_tag "105.jpg"%>
<%=image_tag "106.jpg"%>
<%=image_tag "107.jpg"%>
<%=image_tag "108.jpg"%>
<%=image_tag "109.jpg"%>
<%=image_tag "110.jpg"%>
<%=image_tag "111.jpg"%>
<%=image_tag "112.jpg"%>
</div>
</div>
production.rb
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
Heroku stack:
aspen-mri-1.8.6
bamboo-mri-1.9.2
bamboo-ree-1.8.7
* cedar (beta)
On config/environments/production.rb, check this configuration:
config.serve_static_assets
My production.rb looks like this(worth a try):
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
I found this to work for me because i precompile the assets in development(i have followed the article here and i use option 1. to precompile locally in development). This is what i do
1. First precompile assets in development run: "bundle exec rake assets:precompile"
"A public/assets directory will be created. Inside this directory you’ll find a manifest.yml which includes the md5sums of the compiled assets." - source: heroku docs
2. Then save to git and push to heroku
See if you still get same error in production app(if yes, then i am not sure what to do).
In my app whenever i precompile assets locally this causes my development stylesheets and javascript to go crazy :( . To solve go open environment/development.rb and change this line:
config.assets.debug = true
to
config.assets.debug = false
Also remove the public/assets directory that assets precompile created locally. Restart server and hopefully it should work again. It seems to be a tricky thing to figure out correctly(asset pipeline), but hopefully this will help.

Resources