【问题标题】:NoMethodError in Pages#welcome after installing the bootstrap gem安装引导 gem 后 Pages#welcome 中的 NoMethodError
【发布时间】:2014-03-16 11:48:59
【问题描述】:

在我安装 bootstrap gem 并再次运行 rails server 后,我收到此错误:

NoMethodError in Pages#welcome
undefined method `environment' for nil:NilClass
  (in /Users/neilpatel/Desktop/Rails/prospects/app/assets/stylesheets/custom.css.scss)

custom.css.scss

@import "bootstrap";

app/views/layouts/application.html.erb

<!DOCTYPE html>

<html>
    <head>
      <title>Prospects</title>

      <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
      <%= javascript_include_tag "application", "data-turbolinks-track" => true %>

      <%= csrf_meta_tags %>
    </head>

    <body>
        <%= link_to "Home", "/" %>
        <%= link_to "About", "/about" %>

     <%= yield %>
    </body>
</html>

log/development.log

Processing by PagesController#welcome as HTML
  Rendered pages/welcome.html.erb within layouts/application (0.1ms)
Completed 500 Internal Server Error in 15ms

ActionView::Template::Error (undefined method `environment' for nil:NilClass
  (in /Users/neilpatel/Desktop/Rails/prospects/app/assets/stylesheets/custom.css.scss)):
    4:  <head>
    5:    <title>Prospects</title>
    6: 
    7:    <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
    8:    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    9: 
   10:    <%= csrf_meta_tags %>
  app/views/layouts/application.html.erb:7:in `_app_views_layouts_application_html_erb___3923576615849785590_70354614228980'

宝石文件

source 'https://rubygems.org'

gem 'rails', '4.0.0.rc1'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.0.rc1'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.0.1'
gem 'bootstrap-sass', '~> 3.1.1'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

app/assets/javascripts/application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .

更新了 gemfile 运行包更新

'ran bundle update Bundler could not find compatible versions for gem "railties":
  In Gemfile:
    sass-rails (~> 4.0.2) ruby depends on
      railties (< 5.0, >= 4.0.0) ruby

    rails (= 4.0.0.rc1) ruby depends on
      railties (4.0.0.rc1)

【问题讨论】:

    标签: css ruby-on-rails ruby twitter-bootstrap


    【解决方案1】:

    这是sass-rails gem 如何与在 Sprockets v2.11.1 发布后出现的 sprockets gem 集成的问题(请参阅herehere)。

    要修复它,请更改 Gemfile:

    gem 'sass-rails', '~> 4.0.2'
    

    然后运行bundle update

    注意:您还应该更新 Rails 版本。最新版本是v4.0.4。为此,请将您的 Gemfile 更改为

    gem 'rails', '~> 4.0.0'
    

    再次运行bundle update

    【讨论】:

    • 是的,在我更新 rails gem 后现在可以工作,将来如果我使用 boostrap cdn 并将链接复制到我的应用程序文件夹会更好,使用 cdn 方法有什么优缺点吗? ?谢谢..尼尔
    • 使用(或不使用)CDN 取决于您的需要。您会自定义 Bootstrap(例如不同的颜色、网格尺寸)还是计划集成 theme?您是否关心用户的隐私和/或您想使用 HTTPS?这只是反对 CDN 的原因的简短列表。另一方面有一些好处,但我认为这值得一个单独的问题......
    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 2011-05-07
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多