user2990147
user2990147

Reputation: 31

Heroku doesn't load precompiled assets

I have a staging server on Heroku with the same code pushed to it as my production server (also on Heroku) and none of the assets load in staging, but they all work in prod. Rails 3.2.11.

The assets:precompile works and I can log in with bash and see all of the assets precompiled under public/assets. But I keep getting 404s when I try to view them.

The logs get a whole lot of this:

ActionController::RoutingError (No route matches [GET] "/assets/team_accessories_ad.png"):

I feel like I've tried just about everything. Even though the files are there, it's not routing to public/assets to get them. Am I doing something obviously wrong?

Thanks

EDIT: In my case it was caused by the 12factor gem (https://github.com/heroku/rails_12factor). When I removed this gem, everything started working!

Thanks

Upvotes: 3

Views: 416

Answers (2)

Mehdi Khalili
Mehdi Khalili

Reputation: 937

I had the same problem and it turned out to be caused by changes on how Rails 4 handles static files. I added rails_12factor gem to my production group and it fixed the issue. I think I could've gotten away by just adding rails_serve_static_assets but didn't try that.

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76774

Your problem is that your images are not dynamically linked in your asset files:


SCSS

Rails comes bundled with SCSS because this allows you to precompile the image assets correctly. SCSS is dynamically rendered when you use rake assets:precompile, whereas CSS cannot dynamically render the assets

Because Rails fingerprints the assets when they are precompiled, it means that many of the image assets (which will have a name of XXXX.png before precompile), will have broken paths after the precompile (makes them XXX-dsdasdfew5823459432jt342j52435.png)

The solution is to use dynamic image paths in scss:

#app/assets/stylesheets/images.css.scss
body {
    background: asset_url('image_path.png')
}

This allows you to dynamically render the image path at precompile, giving you the ability to see the image in production

Upvotes: 0

Related Questions