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

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

问题描述

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

我在README https://github.com/thomas-mcdonald/bootstrap-sass 我无法弄清楚并很可能成为我的问题的步骤如下。由于Rails中的更改会阻止在vendor和lib中编译图像,因此您需要将以下行添加到application.rb中:

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

由于我对编程还很陌生,第一个问题是我不知道在application.rb文件中的何处以及如何添加它。我非常感谢,如果有人能帮助告诉我如何以及在哪里正确地添加上面的代码行。



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



预先感谢您的任何帮助,您可以提供。



编辑:添加我第一次尝试的步骤,这样可以在本地主机上正确运行样式,但是代码不会部署到heroku。 / p>


  1. 创建一个新的rails 4应用程序(下面的gem文件)
  2. 添加bootstrap-sass gem在下面的gem文件中

  3. 将PG gem添加到生产组中的gem文件中,并将SQLite3移至开发和测试(运行bundle install - 在生产之后执行步骤2和3) li>
  4. 为静态主页创建页面控制器
  5. 在主页上的英雄单元中添加h1以查看风格是否正常工作
  6. li>
  7. 添加了styles.css.scss文件并包含@import'bootstrap';添加到样式表

  8. 创建git仓库,运行我的初始提交并将代码推送到git

  9. 创建heroku应用程序并将主人推送到heroku

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

  RAILS_ENV =生产捆绑包exec rake资产:预编译

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

Gemfile:

 来源'https://rubygems.org' 

ruby​​2.0.0

#捆绑边缘代替:gem'rails',github:'rails / rails'
gem'rails',' 4.0.0.beta1'

group:production do
gem'pg'
end

group:development,:test do
gem'sqlite3'
结束

#默认情况下,Gems仅用于资产并且不需要
#在生产环境中。
group:assets do
gem'sass-rails','〜> 4.0.0.beta1'
gem'coffee-rails','〜> '4.0.0.beta1'

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

#请参阅https://github.com/sstephenson/execjs#readme了解更多受支持的运行时间
#gem'therubyracer',平台:: ruby​​

gem'uglifier','> = 1.0.3'
结束

gem'jquery-rails'

#Turbolinks在您的以下链接Web应用程序更快。阅读更多:https://github.com/rails/turbolinks
gem'turbolinks'

#轻松构建JSON API。阅读更多:https://github.com/rails/jbuilder
gem'jbuilder','〜> 1.0.1'

#使用ActiveModel has_secure_password
#gem'bcrypt-ruby','〜> 3.0.0'

#使用独角兽作为应用服务器
#gem'unicorn'

#使用Capistrano部署
#gem'capistrano', group::development

#使用调试器
#gem'debugger'


这就是我如何从我的Heroku应用程序中的本地主机上获得我的css显示。



您只需打开生产中的资产服务并设置该记录器可以让Rails4在Heroku上工作,目前我们正在努力平滑Rails 4应用程序的部署过程,但与此同时,您可以在代码中更改这些行,而不需要那些宝石。 (感谢Bret和Neil的好消息)

在/ config / environments / production中。 set:

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

我不知道记录器中的stdout所以不能检查。



做一个git add。和git commit。



确保/config/database.yml包含:

 生产:
适配器:postgresql
编码:unicode
数据库:Your_appname_production

您需要以下env命令的相关信息。



确保您的Gemfile中有gem'pg'生产
Do另一个git提交。



在您的应用程序的终端中,在一行中运行以下命令:

  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似乎只运行生产。



我被告知并且不需要:

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



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


$ b $ pre $ Bundle.require(* Rails.groups(资产:%w(开发测试生产)))

无法记住我在哪里看到该建议。



HTH
Arel


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.

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)

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.

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.

Edit: 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. Created a new rails 4 app (gem file below)
  2. Added the bootstrap-sass gem listed in the gem file below
  3. Added PG gem to my gem file in the production group and moved SQLite3 to development and test (ran bundle install --without production following steps 2 and 3)
  4. created a pages controller for a static home page
  5. Added an h1 within a hero-unit on the home page just to see if style was working
  6. added a styles.css.scss file and included @import 'bootstrap'; to the style sheet
  7. Created git repository, ran my initial commit and pushed the code to git
  8. Created heroku app and pushed the master to heroku

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

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.

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'

解决方案

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.

"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)

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.

Do a git add . and git commit.

Make sure that /config/database.yml has:

production:
  adapter: postgresql
  encoding: unicode
  database: Your_appname_production

You will need this info for the env command below.

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

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

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 Arel

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

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