2013-04-29 69 views
36

我已经为Heroku部署了一个应用程序,但似乎无法解决一个问题。通过Bootstrap-sass应用程序的CSS不加载,因此我有一个无风格的应用程序。目前这只是一个静态页面的集合。使用Rails 4与bootstrap-sass gem无法获得CSS在Heroku上的工作

我在README中关注了除了一步以外的所有步骤https://github.com/thomas-mcdonald/bootstrap-sass我无法弄清楚并很可能成为我的问题的步骤如下。由于防止图片来自于供应商和lib正在编译Rails的变化,你需要将下面的行添加到您的application.rb中:

config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif) 

因为我还是很新的节目,第一问题是我不知道在哪里以及如何在application.rb文件中添加它。如果有人能帮助告诉我如何以及在何处正确添加上面的代码行,我将不胜感激。

第二个问题可能与我正在使用的宝石有关,但是当我创建应用程序时,sass-rails gem被安装了〜> 4.0.0.beta1。根据自述文件,使用的版本是3.2。由于这也可能是一个问题,我已经包括了宝石文件incase任何人都认为这是我的问题的根本原因。

非常感谢您提供任何帮助。

编辑:要添加第一次尝试导致样式在我的本地主机上正常工作的步骤,但是不会将代码部署到heroku。

  1. 创建一个新的轨道4的应用程序(下文宝石文件)
  2. 下面加
  3. 新增PG宝石中的宝石文件中列出的制作组在我的宝石文件和移动sqlite3的自举,萨斯宝石开发和测试(运行软件包安装 - 无需按照步骤2和步骤3进行生产)
  4. 为静态主页创建页面控制器
  5. 在主页上的英雄单元中添加h1以查看样式是否正在工作
  6. add编辑styles.css.scss文件并包含@import'bootstrap';样式表
  7. 的git存放区,跑到我最初的承诺,推动了代码与git
  8. 创建Heroku的应用,推动了主人的Heroku

在第二次尝试,我添加了一个导航栏,以主页(如果这对任何人都有影响),并再次执行步骤7和8,但在执行这些步骤之前,我运行了以下代码行。

RAILS_ENV=production bundle exec rake assets:precompile 

我仍然结束了一个网站,在我的本地主机上有适当的风格,但没有风格在Heroku上工作。正如我在以前的文章中提到的,有一行代码需要添加到application.rb文件中,由于我不了解如何将代码行正确添加到文件中,所以我没有遵循。

的Gemfile:

source 'https://rubygems.org' 

ruby "2.0.0" 

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '4.0.0.beta1' 

group :production do 
gem 'pg' 
end 

group :development, :test do 
gem 'sqlite3' 
end 

# Gems used only for assets and not required 
# in production environments by default. 
group :assets do 
gem 'sass-rails', '~> 4.0.0.beta1' 
gem 'coffee-rails', '~> 4.0.0.beta1' 

gem 'bootstrap-sass', '~> 2.3.1.1' 

# See https://github.com/sstephenson/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 

gem 'uglifier', '>= 1.0.3' 
end 

gem 'jquery-rails' 

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks 
gem 'turbolinks' 

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 1.0.1' 

# To use ActiveModel has_secure_password 
# gem 'bcrypt-ruby', '~> 3.0.0' 

# Use unicorn as the app server 
# gem 'unicorn' 

# Deploy with Capistrano 
# gem 'capistrano', group: :development 

# To use debugger 
# gem 'debugger' 
+0

你有没有预先编译css,然后推送到heroku – 2013-04-29 06:22:32

+0

目前还不清楚你做了什么(除了“按照这些说明),你能写下你到底做了什么? – 2013-04-29 09:06:37

+0

@AndyHayden我在步骤中添加了尝试 – 2013-04-30 20:54:08

回答

70

我刚才(2013年6月13日)得到了Heroku开发者的回答,他们的支持指导我跨越障碍。 这是我从我的Heroku应用程序中使用localhost工作得到的CSS显示。

“你需要做的就是开启生产环境中的资产服务,并将记录器设置为标准输出以使Rails4在Heroku上工作。目前,我们正在努力平滑Rails 4应用程序的部署过程,但同时,你可以在你的代码中改变这些行,而不需要那些宝石。“ (感谢布雷特和尼尔的好消息)

In/config/environments/production。设置:

config.cache_classes = true 
config.serve_static_files = true 
config.assets.compile = true 
config.assets.digest = true 

我不知道记录器中的标准输出,所以不能检查。

做一个git add。和git commit。

确保/config/database.yml有:

production: 
    adapter: postgresql 
    encoding: unicode 
    database: Your_appname_production 

您将需要以下env命令此信息。

确保你在Gemfile中有生产中的宝石'pg' 做另一个git commit。

运行此命令在您的应用程序在终端上一行:

env RAILS_ENV=production DATABASE_URL=postgresql://user:[email protected]/Your_app_name_production bundle exec rake assets:precompile 2>&1 

其中DATABASE_URL = PostgreSQL是相同的YML文件您的生产适配器 和Your_app_name_production指定因为Heroku上似乎只运行生产。

,我被告知反对,也没必要:在束

group :production do 
    gem 'rails_log_stdout',   github: 'heroku/rails_log_stdout' 
    gem 'rails3_serve_static_assets', github: 'heroku/rails3_serve_static_assets' 
end 

它的错误了安装和Heroku的。

不知道这是否帮助,但我还添加了产量

Bundler.require(*Rails.groups(assets: %w(development test production))) 

不记得在那里我看到了他的意见。

HTH 阿雷尔

+20

'config.assets。编译= true'是我失踪的步骤 – andreofthecape 2013-06-26 17:45:56

+1

对我来说,这是所有这些坏男孩的四个: 'config.cache_classes = true' 'config.serve_static_assets = true' 'config.assets.compile = true ' 'config.assets.digest = true' – Baub 2013-08-10 08:16:04

+0

我修改了'production.rb'和'database.yml',然后运行命令,现在工作得很好。你多久执行一次该命令? – JGallardo 2013-11-08 01:09:11

8

我能够通过添加这两种宝石我的应用程序

group :production do 
    gem 'rails_log_stdout',   github: 'heroku/rails_log_stdout' 
    gem 'rails3_serve_static_assets', github: 'heroku/rails3_serve_static_assets' 
end 

来解决这个问题补充一点,运行bundle install,然后推到Heroku的。

你的款式应该开始加载。

+0

谢谢,解决了我的问题,即使所有东西似乎都可以预编译,但没有找到资源(404)。 – code0100fun 2013-05-14 04:23:28

+0

您可能需要考虑在生产组中包装这两行,因为它可能最终导致其他本地实例出错。 – DavidVII 2013-05-14 04:56:55

+1

这里究竟发生了什么? – JGallardo 2013-11-08 01:09:30

2
config.cache_classes = true 
config.serve_static_assets = true 
config.assets.compile = true 
config.assets.digest = true 

在配置/ envirnoments /生产设置这些。RB固定一个类似的问题,我与Apache服务器

18

只是推到Heroku的

+1

这样做对我来说,即使所有其他的东西都设置为true,我不得不做这一步。 – DnfD 2014-04-16 04:48:22

+0

这也适用于我!谢谢! – 2014-10-26 06:49:30

+0

+1和很多谢谢,@JasmineOT!希望能够更多地关注您的(更新的)解决方案。当所有其他都失败时,这也适用于我。这里有一个正式的Heroku Docs的链接,说的相同(对于那些挖掘那种事情的人:https://devcenter.heroku.com/articles/rails-asset-pipeline) – Jaime 2014-11-03 17:21:19

2

我不会设置config.assets.compile = true这会对性能产生影响(但它的工作)之前运行bundle exec rake assets:precompile

由于这里概述: https://stackoverflow.com/a/16882028/647427

When using the asset pipeline, paths to assets must be re-written and sass-rails provides -url and -path helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet. 

image-url("rails.png") becomes url(/assets/rails.png) 
image-path("rails.png") becomes "/assets/rails.png" 
The more generic form can also be used but the asset path and class must both be specified: 

asset-url("rails.png", image) becomes url(/assets/rails.png) 
asset-path("rails.png", image) becomes "/assets/rails.png" 
4

首先升级的Rails中测试到the latest release

检查你可能在哪里设置config.assets.initialize_on_precompile = false,因为这可能会使其回退到非链轮资源分辨率(我猜你可能已经在Heroku文档中阅读Rails 3.x时将其设置为false)。

把它设回默认true

ruby config.assets.initialize_on_precompile = true

然后启用user-env-compile用于在Heroku上的应用:

# Enable precompile support for the app 
heroku labs:enable user-env-compile 
# Remove precompiled assets 
rm -rf public/assets/ 
git add -u 
git commit -m 'Remove precompiled assets' 
# Now push and everything should just work from now on 
git push heroku master 

this bootstrap-sass issue comment改编。

4

设置config.assets.compile=true文件/config/environments/production.rb在:

config.assets.compile=true 

Click here to know更多有关资产的管道。

2

这个英雄问题的一个简单的原因可能是混合的CSS文件类型。根据我自己的经验,如果您推出包含.css和.scss文件类型的资产文件夹,就会发生这种情况。也许别人可以解释为什么会发生这种情况......但是,所有这一切都是为了将​​.css文件重命名为.scss。然后,一切都编译正确,一切都是正确的。

相关问题