[英]Stylesheets, images and javascript are not loading on heroku server in rails4
Hello I have included following in my gemfile 您好,我的gemfile中包含以下内容
group :production, :staging do
gem 'rails_12factor'
end
and inmy stylesheets i have included images as mytest.css.scss 和我的样式表中,我将图像包括为mytest.css.scss
.abc{background: image-url('/assets/mobile/phone.png');}
in production.rb 在production.rb中
config.serve_static_assets = true
config.assets.compile = true
and in herokurun bash I have rake assets:precompile also but still its not loading images , stylesheets and javascripts. 在herokurun bash中,我有rake资产:也可以预编译,但仍然不加载图像,样式表和javascript。 Please guide me how to solve this issue
请指导我如何解决这个问题
Try one of these: 尝试以下方法之一:
background-image: image-url("logo.png")
background-image: asset-url("logo.png", image)
background-image: asset-url($asset, $asset-type)
background-image: asset-data-url("logo.png")
Sometimes, Heroku takes time to recognize new assets - we've found that if you restart the dyno after precompilation, it often loads them on refresh: heroku restart
有时,Heroku需要花费一些时间来识别新资产-我们发现,如果在预编译后重新启动dyno,通常会在刷新时加载它们:
heroku restart
You should accompany this with the referenced dynamic helpers (as per sadaf2605
's answer: 您应该将其与引用的动态助手一起使用(按照
sadaf2605
的回答:
#app/assets/stylesheets/application.js
.element {
background: asset_url("your_image_url.png");
}
The other thing you need to do is precompile for production
-- 您需要做的另一件事是针对
production
进行预编译-
rake assets:precompile RAILS_ENV=production
Debug 调试
It's tough to see what the issue might be; 很难知道问题出在哪里; the main way we debug this is as follows:
我们调试的主要方法如下:
right-click
on your app and view source
right-click
您的应用并view source
console
, you'll probably see a series of 404
( resource not found
) errors. console
,您可能会看到一系列404
( resource not found
)错误。 If the app is looking for fingerprinted files, it's a good sign (it means the files you have are incorrect). 如果应用程序正在寻找指纹文件,则表明它是一个好兆头(表示您的文件不正确)。 To resolve this, you should just precompile before sending to Heroku:
要解决此问题,您应该在发送给Heroku 之前进行预编译:
$ rake assets:precompile RAILS_ENV=production
$ git add .
$ git commit -a -m "Fix"
$ git push heroku master
This will send the right files to Heroku. 这会将正确的文件发送到Heroku。
If your app is not looking for the fingerprinted files, it means you have to reference them properly in your CSS. 如果您的应用程序不查找指纹文件,则意味着您必须在CSS中正确引用它们。 I think you already know about this (written above).
我想您已经知道了(上面写的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.