Reputation: 1253
I have a ruby on rails application where I use some images in a javascript file, like this:
var urlshapes = "/assets/shapes/";
// image loader
imageURLs.push(urlshapes + "isit.png");
imageURLs.push(urlshapes + "ec.png");
imageURLs.push(urlshapes + "bc.png");
imageURLs.push(urlshapes + "bb.png");
imageURLs.push(urlshapes + "io.png");
loadAllImages();
Using the WEBrick server it works, but when I upload to the heroku server I get this errors:
I have read this https://devcenter.heroku.com/articles/rails-4-asset-pipeline, and in result I added this line gem 'rails_12factor', group: :production
in my Gem file, but I still get the errors.
What should I do?
Rails version: 4.1.5 Ruby version: 2.1.2
Upvotes: 0
Views: 91
Reputation: 2963
Seems like you use the asset pipeline. This means assets have different paths between development and production. You need to use the image path helpers to get the correct path/URL on every environment. The image_path helper is probably what you need. The problem is this is a Ruby method and you want to call it in Javascript code. There are 2 solutions:
imageURLs.push("<%= image_path('isit.png') %>")
<script>
tag. e.g.:
<div data-images="['<%= image_path('1.png') %>', '<%= image_path('2.png') ']">
or
<%= javascript_tag("images = ['#{image_path("1.png")}']") %>
and so on.
Upvotes: 1
Reputation: 1253
I put everything in the public folder and changed to this:
var urlshapes = "/shapes/";
// image loader
imageURLs.push(urlshapes + "isit.png");
imageURLs.push(urlshapes + "ec.png");
imageURLs.push(urlshapes + "bc.png");
imageURLs.push(urlshapes + "bb.png");
imageURLs.push(urlshapes + "io.png");
loadAllImages();
Upvotes: 0
Reputation: 1683
In the config/environments/production.rb, did you change this to true, as so:
config.serve_static_assets = true
Heroku pre-compiles the assets into public/assets, so you need to change the config settings.
Upvotes: 0