【问题标题】:Error in loading images in heroku server在heroku服务器中加载图像时出错
【发布时间】:2014-11-12 13:15:56
【问题描述】:

我有一个 ruby​​ on rails 应用程序,我在 javascript 文件中使用了一些图像,如下所示:

  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();

使用 WEBrick 服务器它可以工作,但是当我上传到 heroku 服务器时,我得到了这个错误:

我已阅读此 https://devcenter.heroku.com/articles/rails-4-asset-pipeline,结果我在我的 Gem 文件中添加了这一行 gem 'rails_12factor', group: :production,但我仍然收到错误。

我该怎么办?

Rails 版本:4.1.5 Ruby 版本:2.1.2

【问题讨论】:

  • 您是否尝试在另一个目录中手动访问这些资产?像 http://.herokuapp.com/shapes/bc.png 等等?个人建议把这些文件放到public目录下
  • 如果我手动尝试,我会得到这个“您要查找的页面不存在。”。我的问题是为什么?我将在公共文件夹中尝试它。公共文件夹中的 url 应该是什么?
  • 只是 http://.herokuapp.com/ 。检查来自 Suomi 的第一个答案。这可能是一个不错的起点。
  • 我把所有东西都放在了公用文件夹中,现在它运行良好。

标签: javascript ruby-on-rails ruby ruby-on-rails-4 heroku


【解决方案1】:

在 config/environments/production.rb 中,您是否将其更改为 true,如下所示:

config.serve_static_assets = true

Heroku 将资产预编译为 public/assets,因此您需要更改配置设置。

【讨论】:

【解决方案2】:

我把所有的东西都放在公用文件夹里,改成这样:

  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();

【讨论】:

    【解决方案3】:

    好像您使用的是asset pipeline。这意味着资产在开发和生产之间有不同的路径。您需要使用 image path helpers 在每个环境中获取正确的路径/URL。 image_path 助手可能是您需要的。问题是这是一个 Ruby 方法,你想在 Javascript 代码中调用它。有两种解决方案:

    • 在您的 javascript 文件名末尾附加 .erb。然后,您可以像在视图中一样使用 ERB。例如imageURLs.push("<%= image_path('isit.png') %>")
    • 如果您的 Javascript 代码是从 HTML 页面加载的,请在视图中添加图像 URL 数组,然后通过 Javascript 引用它们。您可以向 HTML 元素添加数据属性,也可以简单地添加 <script> 标记。例如:

    <div data-images="['<%= image_path('1.png') %>', '<%= image_path('2.png') ']"> 要么 <%= javascript_tag("images = ['#{image_path("1.png")}']") %> 等等。

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 2019-07-19
      • 2014-07-23
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多