无法使用带有 bootstrap-sass gem 的 Rails 4 在 Heroku 上使用 CSS [英] Can't get CSS working on Heroku using Rails 4 with bootstrap-sass gem

查看:19
本文介绍了无法使用带有 bootstrap-sass gem 的 Rails 4 在 Heroku 上使用 CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经向 Heroku 部署了一个应用程序,但有一个我似乎无法解决的问题.通过 Bootstrap-sass 应用程序的 CSS 没有加载,因此我有一个无样式的应用程序.目前这只是静态页面的集合.

I have deployed an app to Heroku with one issue I can't seem to get figured out. The CSS for the app via Bootstrap-sass does not load up thus I have an un-styled app. At the moment this is just a collection of static pages.

我遵循了自述文件中的所有步骤 https://github.com/thomas-mcdonald/bootstrap-sass 我无法弄清楚并且很可能是我的问题的步骤如下.由于 Rails 中的一项更改会阻止在供应商和库中编译图像,因此您需要将以下行添加到您的 application.rb 中:

I have followed all but one step in the README https://github.com/thomas-mcdonald/bootstrap-sass The step I can't figure out and highly likely to be my issue is as follows. Due to a change in Rails that prevents images from being compiled in vendor and lib, you'll need to add the following line to your application.rb:

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

由于我对编程还是很陌生,第一个问题是我不知道在 application.rb 文件中的何处以及如何添加它.如果有人能帮助我展示如何以及在何处正确添加上述代码行,我将不胜感激.

Since I am still very new to programming, the first issue is I have no clue where and how to add this within the application.rb file. I would greatly appreciate it if someone could help show me how and where to properly add the above line of code.

第二个问题可能与我使用的 gems 有关,但是当我创建应用程序时,sass-rails gem 安装在 ~> 4.0.0.beta1 中.根据自述文件,使用的版本是 3.2.由于这也可能是一个问题,因此我包含了 gem 文件,以防有人确定这是我问题的根本原因.

The second issue could be related to the gems I am using however when I created the app, the sass-rails gem was installed with ~> 4.0.0.beta1. According to the README the version to use is 3.2. Since this also might be an issue, I have included the gem file incase anyone determines that is the underlying reason for my problem.

预先感谢您提供的任何帮助.

Thank you in advance for any help you can provide.

添加我在第一次尝试时采取的步骤,导致样式在我的本地主机上正常工作,但在将代码部署到 heroku 后就不行了.

To add the steps I took on the first try that resulted in style working properly on my local host but not once the code was deployed to heroku.

  1. 创建了一个新的 rails 4 应用程序(下面的 gem 文件)
  2. 添加了下面 gem 文件中列出的 bootstrap-sass gem
  3. 在生产组中将 PG gem 添加到我的 gem 文件中,并将 SQLite3 移至开发和测试(运行 bundle install --without production 以下步骤 2 和 3)
  4. 为静态主页创建了一个页面控制器
  5. 在主页上的英雄单元中添加了一个 h1,以查看样式是否有效
  6. 添加了一个styles.css.scss文件并包含了@import 'bootstrap';到样式表
  7. 创建 git 存储库,运行我的初始提交并将代码推送到 git
  8. 创建 heroku 应用程序并将 master 推送到 heroku

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

On the second attempt, I added a nav bar to the home page (if that makes a difference to anyone) and followed steps 7 and 8 again but just prior to doing those steps I ran the following line of code.

RAILS_ENV=production bundle exec rake assets:precompile

我最终还是得到了一个在我的本地主机上具有适当风格的网站,但在 Heroku 上没有任何风格.正如我在上面的原始帖子中所指出的,有一行代码需要添加到 application.rb 文件中,由于我不了解如何将这行代码正确添加到文件中,因此我没有遵循.

I still ended up with a site that had the proper style on my local host but no style was working on Heroku. As I noted above in my original post, there is a line of code that needs to be added to the application.rb file that I did not follow due to my lack of understanding how to properly add the line of code into the file.

宝石文件:

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'

推荐答案

我刚刚(2013 年 6 月 13 日)从 Heroku 开发人员那里得到了这个答案,他们的支持引导我跨越了障碍.这就是我在 Heroku 应用程序中从 localhost 获取 css 显示的方式.

I just now (June 13 2013)got this answer from Heroku devs whose support guided me across the barriers. This is how I got my css display from localhost working in my Heroku app.

您需要做的就是在生产中打开资产服务并将记录器设置为 stdout 以使 Rails4 在 Heroku 上工作.我们目前正在努力使 Rails 4 应用程序的部署过程变得顺畅,但与此同时,您只需更改代码中的那些行,您就不需要这些宝石了."(感谢布雷特和尼尔的好消息)

"All you need to do is turn on asset serving in production and set the logger to stdout to get Rails4 to work on Heroku. We are currently working on smoothing out the deploy process for Rails 4 apps but for the meantime, you can just change those lines in your code and you won't need those gems." (Thanks Bret and Neil great news)

在/config/environments/production 中.设置:

In /config/environments/production. set:

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

我不知道记录器中的标准输出,所以无法检查.

I do not know about the stdout in logger so can't check that.

做一个 git add .和 git commit.

Do a git add . and git commit.

确保/config/database.yml 具有:

Make sure that /config/database.yml has:

production:
  adapter: postgresql
  encoding: unicode
  database: Your_appname_production

您将需要此信息用于下面的 env 命令.

You will need this info for the env command below.

确保你的 Gemfile 中有 gem 'pg'再做一次 git commit.

Make sure you have gem 'pg' in production in your Gemfile Do another git commit.

在您应用的终端中的一行运行此命令:

Run this command in a terminal in your app on one line:

env RAILS_ENV=production DATABASE_URL=postgresql://user:pass@127.0.0.1/Your_app_name_production bundle exec rake assets:precompile 2>&1

其中 DATABASE_URL=postgresql 与 yml 文件中的生产适配器相同并且指定了 Your_app_name_production 是因为 Heroku 似乎只运行生产.

Where DATABASE_URL=postgresql is identical to your production adapter in the yml file and Your_app_name_production is specified because Heroku only seems to run production.

有人建议我不要,也不需要:

I was advised against and did not need:

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 中出错.

It errors out in bundle install and Heroku.

不知道这是否有帮助,但我也将生产添加到

Don't know if this helps but I also added production to

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

不记得我在哪里看到的建议.

Can't remember where I saw that advice.

HTH阿雷尔

这篇关于无法使用带有 bootstrap-sass gem 的 Rails 4 在 Heroku 上使用 CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆