Question

I'm trying to deploy my first Rails application, but I'm getting 404 Not found for all assets (.css, .js files and images). I tried many ways of getting things work, but nothing worked. All of the asset paths look like this:

/stylesheets/application.css

My production.rb file: http://pastebin.com/SeVNEZD9 My application.html.erb, where I include these assets look like this: http://pastebin.com/gHkpfA8Z Thanks forward for any help.

Was it helpful?

Solution

In config/environments/production.rb file add

 config.assets.digest = true

Then stylesheet_link_tag will generate fingerprints url.

OTHER TIPS

You need to precompile your assets for this by running this command on terminal.

RAILS_ENV=production bundle exec rake assets:precompile

After deployment you should do

RAILS_ENV=production bundle exec rake assets:precompile

Since this is your first Rails app, let me explain where your problem might be:

When you create your assets, they reside in the asset pipeline. This is a folder located at /app/assets where your asset files will be stored

If you want to use assets in production, there are two ways to do it - the first is to use the assets dynamically, or to use them as static (which is where you precompile). When you use the likes of Heroku etc, you need to use static assets, which is why they precompile them for you when you deploy

The problem with static assets is that Rails creates fingerprints for them. These fingerprints basically give them a unique identifier which you have to dynamically reference using <%= asset_path %> etc

Your problem is likely caused by you not accessing the precompiled assets correctly. The way to fix this is to use asset tag helpers like this:

#app/views/layouts/application.html.erb
<%= stylesheet_link_tag "application" %>
<%= javascriot_include_tag "application" %>

It looks like it's working now. I needed to restart the server to apply the changes. Can anybody tell me why is it that I have to restart the server to apply such changes? I want to avoid it in the future.

I'm posting my production.rb file for those who face similar problems: http://pastebin.com/7Uskvcuc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top