为 Rails 应用程序在本地加载 CSS 但不在 Heroku 中加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12719541/
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
CSS loading locally but not in Heroku for a rails app
提问by pvskisteak5
I'm following a rails prelaunch signup tutorial. Bootstrap was working fine deployed on heroku, but then I modified the bootstrap_and_override file to include a logo. When launching a rails server locally and viewing, the style is as I intended with bootstrap and the new header logo appearing.
我正在学习 rails prelaunch 注册教程。Bootstrap 部署在 heroku 上运行良好,但随后我修改了 bootstrap_and_override 文件以包含徽标。在本地启动 rails 服务器并查看时,样式与我预期的一样,引导程序和新标题徽标出现。
Like I did before, I ran rake assets:precompile and pushed to git - followed this tutorial http://railsapps.github.com/rails-heroku-tutorial.htmlwhich worked before showing the css while on heroku. The html is showing but it looks like css and bootstrap are missing.
就像我以前做的那样,我运行了 rake assets:precompile 并推送到 git - 按照本教程http://railsapps.github.com/rails-heroku-tutorial.html在 Heroku 上显示 css 之前工作。html 正在显示,但看起来缺少 css 和 bootstrap。
Does anyone have any ideas?
有没有人有任何想法?
回答by Connor Leech
Run bundle exec rake assets:precompile
on your local code
bundle exec rake assets:precompile
在本地代码上运行
Commit the changes and deploy to heroku
提交更改并部署到 heroku
回答by Peter J Bond
Had the same exact problem, I don't know if it will help other in the future, but I got this on my "git push heroku master" push:
有同样的确切问题,我不知道它将来是否会帮助其他人,但我在我的“git push heroku master”推送中得到了这个:
WARNINGS:
警告:
Include "rails_12factor" gem to enable all platform features
Updated my gemfile,
更新了我的 gemfile,
*group :production, :staging do
gem 'pg'
gem 'rails_12factor'
end*
Ran it again, to my delight it installed these:
再次运行,令我高兴的是它安装了这些:
Installing rails_serve_static_assets (0.0.1)
Installing rails_stdout_logging (0.0.1)
Installing rails_12factor (0.0.2)
Site worked perfect after that. Hope that helps someone, I'm new to all this.
在那之后,网站运作完美。希望对某人有所帮助,我对这一切都很陌生。
回答by Francisco Quintero
Check that in /config/environments/production.rb you have these set to true
检查在 /config/environments/production.rb 中是否将这些设置为 true
config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true
config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true
git add, git commit and push to heroku.
git add、git commit 和 push 到 heroku。
回答by hellion
If your using sass...before you go through all this other trouble...make sure your not mixing .css and .scss files in your assets folder.
Heroku seems to have some trouble mixing the two when compiling assets. I can't explain why of if its an accurate explanation...but, in my own experience all I had to do to fix this was simply rename any .css files to .scss.
如果你使用 sass ......在你解决所有这些其他问题之前......确保你没有在你的资产文件夹中混合 .css 和 .scss 文件。
Heroku 在编译资产时似乎在将两者混合在一起时遇到了一些麻烦。我无法解释为什么它是一个准确的解释......但是,根据我自己的经验,我所要做的就是解决这个问题,只需将任何 .css 文件重命名为 .scss。
回答by pvskisteak5
Figured it out after trying about everything stackoverflow could suggest. I simply checked git status, saw I had a bunch of uncommitted files (thought this would have been picked up by git push heroku master), then added, committed and pushed to git. Then I started over, ran bundle install, precompiled assets and deployed to heroku and it is working.
在尝试了 stackoverflow 可能建议的所有内容后弄清楚了。我只是检查了 git status,看到我有一堆未提交的文件(认为这会被 git push heroku master 接收),然后添加、提交并推送到 git。然后我重新开始,运行 bundle install,预编译资产并部署到 heroku 并且它正在工作。
回答by praaveen
I got the same problem in rails4 fixed with adding
a. gem 'rails_12factor'
b. bundle exec rake assets:precompile
我在 rails4 中遇到了同样的问题,添加了一个。gem 'rails_12factor'
湾bundle exec rake assets:precompile
回答by Taimoor Changaiz
To load assets in production environment:
要在生产环境中加载资产:
How to load assets in production rails
Note: After following above link
注意:按照上面的链接后
rm -rf tmp/*
rm -rf public/assets/*
RAILS_ENV='production' rake assets:precompile
then restart your server
然后重启你的服务器
回答by Kieran
This issue can also happen when you are cloning an app and using a CDN for your assets.
当您克隆应用程序并为您的资产使用 CDN 时,也会发生此问题。
Comment out config.action_controller.asset_host
in production.rb
if your heroku app is a staging site.
注释掉config.action_controller.asset_host
中production.rb
,如果您的Heroku的应用程序是一个临时网站。
回答by Garry Tiscovschi
This is a long shot, but if you're using Chrome try pressing Ctrl+Shift+Rto reload the page as Chrome can sometimes cache old CSS settings.
这是一个长镜头,但如果您使用 Chrome,请尝试按Ctrl+ Shift+R重新加载页面,因为 Chrome 有时会缓存旧的 CSS 设置。