Ruby-on-rails 在heroku上未加载Rails 4图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18324063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:33:49  来源:igfitidea点击:

Rails 4 images not loading on heroku

ruby-on-railsheroku

提问by brad

I have spent the better part of the day trying to get images to load on my heroku app. Everything I try works locally, but not after being deployed to heroku.

我花了一天的大部分时间试图让图像加载到我的 heroku 应用程序上。我尝试的一切都在本地工作,但在部署到 heroku 之后就不行了。

I have png files saved in the images folder under my assets. I am referencing these images with syntax in my css such as;

我在我的资产下的图像文件夹中保存了 png 文件。我在我的 css 中使用语法引用这些图像,例如;

#signin {
  background: url(<%= asset_path 'sf.png' %>);
  background-size: 100%;
}

In heroku when I inspect the background the assets/sf.png link is there but when you click it it shows a broken image, suggesting it did not load properly.

在 heroku 中,当我检查背景时,assets/sf.png 链接在那里,但是当您单击它时,它显示一个损坏的图像,表明它没有正确加载。

I've tried toggling config.serve_static_assets = falsein the production.rbfile between true and false and neither works.

我试过config.serve_static_assets = falseproduction.rb真假之间切换文件,但都不起作用。

I also have

我也有

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

Precompile is always successful.

预编译总是成功的。

Rails 4. Any ideas on what else to try?

Rails 4. 还有什么可以尝试的想法吗?

采纳答案by brad

Another issue, I was having with this was that I was precompiling my assets locally, prior to loading it to heroku. This requires you to follow a different set of steps, which can be found below. If you precompile your assets locally, you must follow these steps or any updates you made to your assets folder will not be reflected in prod.

我遇到的另一个问题是,在将资产加载到 heroku 之前,我正在本地预编译我的资产。这要求您遵循一组不同的步骤,这些步骤可以在下面找到。如果您在本地预编译您的资产,则必须遵循这些步骤,否则您对资产文件夹所做的任何更新都不会反映在 prod 中。

https://devcenter.heroku.com/articles/rails-asset-pipeline

https://devcenter.heroku.com/articles/rails-asset-pipeline

RAILS_ENV=production bundle exec rake assets:precompile

RAILS_ENV=production bundle exec rake assets:precompile

commitand pushto server.

commitpush服务器。

回答by kal

I needed to combine several solutions to make this work, here is what I did:

我需要结合几个解决方案来完成这项工作,这是我所做的:

Gemfile

文件

gem 'rails_12factor', group: :production

in my Heroku console

在我的Heroku 控制台中

heroku labs:enable user-env-compile -a yourapp

production.rb

生产.rb

config.serve_static_assets = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
config.assets.compile = true

I didn't need to precompile the assets locally.

我不需要在本地预编译资产。

回答by Prabhakar Undurthi

You need to do two things to resolve it. First, change these two lines from false to true in production.rbfile.

你需要做两件事来解决它。首先,将文件中的这两行从 false 更改为 true production.rb

      config.assets.compile = true
      config.assets.digest = true

Second, if you've syntax like this for your images

其次,如果你的图像有这样的语法

    background: url("imgo.jpg") 

Change it to

将其更改为

     background: image-url("image.jpg")

I hope it does your job.

我希望它能完成你的工作。

回答by Percevalve

I had a similar issue and I solved it with the following line in the custom.css.scss.. Tell me if this works for you.

我有一个类似的问题,我用 custom.css.scss 中的以下行解决了它。告诉我这是否适合你。

background: image-url('sf.png')

Referencing the asset being done different ways depending if you are using ERB or Sass, see in the Ruby on Rails Guide.

根据您使用的是 ERB 还是 Sass,不同的方式引用资产,请参阅 Ruby on Rails 指南

回答by alightholder

I don't have the reputation to comment (yet) but it's important to note that the Heroku labs feature has been removed, so now you'll get a "No such feature: user-env-compile" error

我还没有评论的声誉(还),但重要的是要注意 Heroku 实验室功能已被删除,所以现在您将收到“没有这样的功能:user-env-compile”错误

More: https://github.com/Crowdtilt/CrowdtiltOpen/issues/251

更多:https: //github.com/Crowdtilt/CrowdtiltOpen/issues/251

回答by Batz

Rails ('4.1.5') I had a similar issue of images not showing on Heroku but showing up locally. I am not using paperclip or carrierwave gems, I precompile locallyand also using RAILS_ENV=productionI push to github and it deploys fine to Heroku.

Rails ('4.1.5') 我有一个类似的问题,图像没有在 Heroku 上显示,但在本地显示。我没有使用回形针或carrierwave gems,我在本地预编译并使用RAILS_ENV=production我推送到github,它可以很好地部署到Heroku。

I solved the issue by having:

我通过以下方式解决了这个问题:

config.serve_static_assets = true
config.assets.compile = true
config.assets.js_compressor = :uglifier
config.assets.digest = true

// delete precompiled assets
bundle exec rake assets:clobber --trace
RAIL_ENV=production bundle exec rake assets:clobber --trace

copied images to public/assets from app/assets. then:

将图像从应用程序/资产复制到公共/资产。然后:

// tests should pass
bundle exec rake assets:precompile --trace
RAILS_ENV=production bundle exec rake assets:precompile --trace

git commit
git push

And it was running fine on Heroku.

它在 Heroku 上运行良好。

回答by user2903934

I tried many solutions too but i found an invaluable solution and explanation 1. Heroku looks for assets in the public folder and that means you have to pre-compile your assets but if you were like me someone looking for a way to precompile my assets when my development environment is set to gem sqlite and production set to pg then you would do this.

我也尝试了很多解决方案,但我找到了一个非常宝贵的解决方案和解释 1. Heroku 在公共文件夹中查找资产,这意味着您必须预编译您的资产,但如果您像我一样,有人正在寻找预编译我的资产的方法我的开发环境设置为 gem sqlite,生产环境设置为 pg,那么你就可以这样做了。

in your production.rb

在你的 production.rb

config.serve_static_assets = true

if you do not have gem pg installed you need to comment it out and change your production environment to use gem sqlite and run this

如果您没有安装 gem pg,您需要将其注释掉并更改您的生产环境以使用 gem sqlite 并运行它

RAILS_ENV=production bundle exec rake assets:precompile

when all assets have been precompiled, switch back to your default settings and git add .,commit, and push to heroku

当所有资源都被预编译后,切换回你的默认设置和 git add .,commit,然后推送到 heroku

回答by Jorge Diaz

I had a similar problem with showing just images. Being new to rails I did not know I could use:

我在只显示图像时遇到了类似的问题。作为 Rails 的新手,我不知道我可以使用:

<%= image_tag("fileName.png", alt: "File Name Fancy", size: "100x100")%>

<%= image_tag("fileName.png", alt: "File Name Fancy", size: "100x100")%>

Instead of traditional html.

而不是传统的html。

The image_tag is explained in the rails api but I find its use is better explained here: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag

在 rails api 中解释了 image_tag,但我发现它的用法在这里得到了更好的解释:http: //apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag

All I added to my app was this gem: gem 'rails_12factor', group: :production

我添加到我的应用程序的只是这个宝石: gem 'rails_12factor', group: :production

As described in the heroku asset-pipeline documentation. https://devcenter.heroku.com/articles/rails-4-asset-pipeline

如 heroku 资产管道文档中所述。https://devcenter.heroku.com/articles/rails-4-asset-pipeline

回答by Dzung Nguyen

The images won't be served as assets by default, only css and js. You should look into the answer of

默认情况下,图像不会作为资产提供,只有 css 和 js。你应该看看答案

Syed Ehtsham Abbas in this question Heroku does NOT compile files under assets pipelines in Rails 4

Syed Ehtsham Abbas 在这个问题中 Heroku 不会在 Rails 4 中的资产管道下编译文件