Kim
Kim

Reputation: 1057

Asset are not show after deploy - heroku - rails 4

I am developing a test app in rails 4 and would like to host it on heroku. The problem is that I cant seem to get the assets to show while this works fine on my local system.

See the following link: http://depot-test.herokuapp.com/ > all assets have some random id behind the filename.

First I checked of the assets are actually available in the repo, they are.

Then I checked if the assets are available on heroku with bash:

$ heroku run bash

$ ls -la app/assets/images

total 60
drwx------ 2 u41129 41129  4096 2013-09-08 17:27 .
drwx------ 5 u41129 41129  4096 2013-09-08 17:27 ..
-rw------- 1 u41129 41129 15093 2013-09-08 17:27 cs.jpg
-rw------- 1 u41129 41129     0 2013-09-08 17:27 .keep
-rw------- 1 u41129 41129  1040 2013-09-08 17:27 logo.png
-rw------- 1 u41129 41129  1787 2013-09-08 17:27 rails.png
-rw------- 1 u41129 41129  9882 2013-09-08 17:27 rtp.jpg
-rw------- 1 u41129 41129 12549 2013-09-08 17:27 ruby.jpg

And as you see they are.

Then I tried to load an image directly, http://depot-test.herokuapp.com/assets/logo.png > 404

I am wondering what is causing the id's to be added.

UPDATE

The hash is apparently created by rails but still the assets are not being loaded. I am using the standard rails helpers like:

image_tag

See my layout:

<!DOCTYPE html>
<html>
    <head>
        <title>Pragprog Books Online Store</title>
        <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
        <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
        <%= csrf_meta_tags %>
    </head>
    <body class="<%= controller.controller_name %>">
        <div id="banner">
            <%= image_tag("logo.png") %>
            <%= @page_title || "Pragmatic Bookshelf" %>
        </div>
        <div id="columns">
            <div id="side">

                <% if @cart %>
                    <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
                      <%= render @cart %>
                    <% end %>
                <% end %>

                <ul>
                    <li><a href="http://www....">Home</a></li>
                    <li><a href="http://www..../faq">Questions</a></li>
                    <li><a href="http://www..../news">News</a></li>
                    <li><a href="http://www..../contact">Contact</a></li>
                </ul>

                <!-- Exersise of showing current time -->
                <span><%= Time.now.strftime("%I:%M:%S %z") %></span>
                <!-- End showing time -->

            </div>
            <div id="main">
                <%= yield %>
            </div>
        </div>
    </body>
</html>

Upvotes: 2

Views: 1885

Answers (2)

Kim
Kim

Reputation: 1057

In the heroku rails 4 startup guide, https://devcenter.heroku.com/articles/rails4#logging-and-assets it states that you need a gem called:

gem 'rails_12factor', group: :production

Without this gem the asset pipeline will not work. This fixed my problem.

Upvotes: 3

pdobb
pdobb

Reputation: 18037

The random id behind the filename you're seeing is the MD5 fingerprint, which is added in production mode. This is a cache-busting technique. So you can't actually refer to an asset by filename alone in production mode. So, wherever you're trying to refer to the asset, you need to use asset_path at the least. Or, if you have an image then image_tag will do the right thing for you.

Here's a better explanation for why the MD5 fingerprint is a good thing: http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care-questionmark

Here's info on the available helpers you must use to include the proper MD5 fingerprint in your asset paths: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

Upvotes: 1

Related Questions