Rails - image.png Isn't Precompiled - ruby-on-rails

I'm attempting to deploy a web application on Amazon's EC2 servers, and I have the code up on the server. Everything looks like it's working, but when I go to the home page, I get a 500 error message and the production.log file gives me the following error:
ActionView::Template::Error (image.png isn't precompiled)
I've tried running rake assets:precompile,
I've changed the line in config/environments/production.rb to config.assets.compile = true
I've checked that the compiled image.png is in public/assets/manifest.yml
but I still get the same error.
I'm running Rails 3.2.6 and Ruby 1.8.7.
Thanks for your help!

Running rake assets:precompile RAILS_ENV=production should fix it.
If you don't provide any RAILS_ENV, Rails assumes it's development
I believe that's because each env in the asset pipeline behaves differently. Therefore, as a general rule, in production, always run rake tasks with RAILS_ENV='production' and you should stay safe.

I am using Openshift from Redhat and when deployed on the server it RAKEs to the production environment automatically. I too had the same problem but only on the production server.
Performing the change:
config/environments/production.rb to config.assets.compile = true
worked a treat. There is another parameter in this file vou could change:
config.assets.precompile += %w[ *.png *.jpeg *.jpg *.gif ]
When deployed on the server you could then do manually:
rake RAILS_ENV=production
or if you are using a server like Openshift that deploys automatically, configurations in the production.rb file will be taken into account.

In config/application.rb add a line like:
config.assets.precompile += ['image.png']
Although, if its in public/assets/images you shouldn't have to.

Resolved - needed to run rake assets:precompile RAILS_ENV='production instead of just rake assets:precompile ...

Related

Rails: CSS changes in developement not visible

I'm using Rails 5.0
Since I've done this command-line : ENV=production rake assets:precompile
when i change CSS I can see them in local instantly.
I have to kill the server, do this command-line again (ENV=production rake assets:precompile) in order to see the change I made.
Thaks for your help.
Rails requires you to pre-compile assets (css,js,etc) in production after you change anything in assets. However in development assets are compiled live by default.
If you want to compile assets live in production (Not Recommended):
then change config.assets.compile=false in config/environments/production.rb to:
config.assets.compile = true
That is Rails should recompile the assets when it detects that a new version of the source assets is there.
On production, you usually want to set it to false and handle asset compilation during deployment. For this, you have to run
RAILS_ENV=production bin/rails assets:precompile
Usually, if you deploy using Capistrano, it takes care of that.
Note: In rails 4 or earlier it was RAILS_ENV=production bin/rake assets:precompile but in Rails 5.x rake is merged into rails so you must use RAILS_ENV=production bin/rails assets:precompile.
In config/environments/development.rb,
I had config.assets.debug set to false instead of true.

How do I get the Rails server to refresh precompiled assets?

I'm testing my Rails 4 app in the production environment on my localhost:3000 using the built in Webrick server. When I run RAILS_ENV=production bundle exec rake assets:precompile the assets are rebuilt in public and the manifest is rebuilt, but the pages are still being served with the previous asset names.
Restarting the rails server makes the new assets appear. Is there a less extreme way to achieve this and how will this behave when I port this to my production server running Phusion Passenger. I really don't want to restart Apache to get my assets in gear.
If you did not change the contents of assets, the precompiled version will be as same as the previous one. If you change it even a bit, the fingerprint will change and app will request for the new one only as you have set config.assets.digest = true.
Anyway another work around would be:
Just run:
rake assets:clean
and then,
rake assets:precompile
This makes everything in the asset pipeline to be rebuilt and serve freshly.
Rails automatically clear the cache for every individual file when its contents are edited.
If any of the above did not work, please try as below:
config.serve_static_assets = true in config/environments/production.rb
config.serve_static_assets configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. Nginx or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
Ref: http://guides.rubyonrails.org/configuring.html#rails-general-configuration
Hope it helps :)
We faced the same problem where the old assets were being served even after trying rake assets:clean or assets:clobber and eventually server reboot would resolve the issue. The culprit in our case was unicorn. While deploying our rails app using mina and mina-unicorn, we ran rake assets:clobber, then compiled assets and then restarted unicorn in the end. By doing this the unicorn master never gets stopped and continues to show old assets. So, we changed our mina deploy script and instead of restarting unicorn, we stopped unicorn and started it back. This resolved the issue. So, the key steps are
Deploy application
run rake assets:clobber
run rake assets:precompile
stop unicorn
start unicorn
This kills the concept of zero downtime but this is a better solution than restarting the server.
I understand that you use passenger but this information can be helpful for others
You can recompile your rails assets by running the following command:
bin/rails assets:precompile RAILS_ENV=development
en:
activemodel: # or activerecord:
errors:
models:
person:
# Override the format for all Person attributes:
format: "Invalid %{attribute} (%{message})"
attributes:
age:
# Override the format for the age attribute:
format: "%{message}"
blank: "Please fill in your %{attribute}"

Rails 3.2 precompile compiles image but ActionView does not pull it

I am deploying a RoR 3.2 app to a shared server.
When I run rake assets:precompile RAILS_ENV=production I can see at ~/public_html/my_app/assets/ that all my images are precompiled.
But when I go to mywebsite.com ActionView throws an error. The production log gives me:
ActionView::Template::Error (200/adrap21.png isn't precompiled).
Funny thing is that at ~/public_html/my_app/assets/200 I can see a precompiled image: adrap21-a5f042dd2b89a3d87eba25969495d678.png
I have run several times one of the following:
1) rake assets:precompile RAILS_ENV=production
2) RAILS_ENV=production rake assets:precompile
at my_app/config/environments/production.rb I have the following:
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
I have changed config.assets.compile from false to true and does not help.
I have read related answers but other people solutions are not working for me.
This one was suposed to be foolproof. Another one, more specific about not compiling images, this other prevents using config.assets.compile = true.
I have even tried the advice of using config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] , but it is not yet working. Might there be something wrong with images call? Is it a RoR 3.2 bug?
FOUND WHAT THE PROBLEM WAS.
Phusion Passenger needs to be restarted for the compiled assets to work. That is why, using Capistrano, you need to restart the server for all the compiled to work together.
Thanks again, Paritosh. I know too how slow is learning curve at beginning when you learn to program by yourself.

Rails asset pipeline: images not looking up resource with build id

I'm getting a 404 on all of the images included with jquery-ui-rails on Rails 4.0.1 after going production. It works fine in development environment. The site is looking for /assets/jquery-ui/ui-icons_222222_256x240.png, but only public/assets/jquery-ui/ui-icons_222222_256x240-890385424135de1513f00cbecfb7f990.png exists in the filesystem. How come production build IDs are not being appended?
I've also had this problem with some fonts. For the time being I've worked around it by just manually copying and pasting to the sought path.
First thing to try is precompiling assets specifically for the production environment:
RAILS_ENV=production rake assets:precompile
If that doesn't do anything, set the following in production.rb and precompile again
config.assets.precompile += ['*.js', '*.css']
config.assets.compile = true

Ruby on Rails: Ran rake assets:precompile and now both local and heroku deployment don't include bootstrap

I was having issues deploying my project to a heroku server (Precompile fail). So I found this response, https://stackoverflow.com/a/13713753/2989437, and followed up on the advice. I added one line to my application.rb file:
application.rb
module FirstEdc
class Application < Rails::Application
config.assets.initialize_on_precompile = false # I added this line
...
end
end
I then ran the precompile command, committed the changes, and managed to deploy successfully to heroku. However, now my bootstrap/css appears to have stopped functioning both on the heroku deployment, and my local deployment.
I learned that I was supposed to add another line to my deployment.rb file:
deployment.rb
FirstEdc::Application.configure do
...
# Allows for local precompilling --added by Ian
config.assets.prefix = '/dev-assets'
end
So I added this, recompiled and redeployed, but to no avail.
Finally, I ran a rake assets:clean in an attempt to at least get my local deployment back to normal, but it did not work.
Any advice would be greatly appreciated. I'm reading more into the asset pipeline now, but I feel like this could be a cache problem or something. I'll update as I figure out what's going on.
edit. Just to clarify, I've tried removing both additions, running a rake assets:clean and rake assets:clean:all, but neither fix my local deployment.
I don't use config.assets.prefix in any of my apps and they work just fine with Heroku, so not sure what that's doing.
Try removing that line, then running rake assets:clean. Now your local server should be using the files as you change them. When you want to push, run rake assets:precompile first, then push.
If you want to make changes locally after that, run rake assets:clean again to get rid of the precompiled files on your local machine.
If Heroku detects any files in public/assets it will not attempt to precompile your assets again. This is by design.
So, you need to make a decision to either always precompile your assets with rake assets:precompile, or remove any files in public/assets before pushing to Heroku.
(The recommended way is to allow Heroku to precompile them during push)

Resources