如何修复错误:Ruby on Rails 中的“Sass::SyntaxError in Posts#index”?

Posted

技术标签:

【中文标题】如何修复错误:Ruby on Rails 中的“Sass::SyntaxError in Posts#index”?【英文标题】:How to fix Error : "Sass::SyntaxError in Posts#index" in Ruby on Rails? 【发布时间】:2017-01-20 04:08:26 【问题描述】:

我在阅读教程时尝试将引导程序添加到我的 Rails 应用程序中,但出现以下错误。请解释发生了什么,并请编辑我的代码以显示如何修复。并且您将参考Rails 上的任何资源以使其变得更好,我们将不胜感激。 附:我才刚开始。感谢您的阅读。 :D

页面:http//localhost:3000/posts

错误:

Sass::SyntaxError in Posts#index

找不到或无法读取要导入的文件:引导程序。加载路径: /Users/mac/Documents/Projects/Web 开发/Ruby On Rails/Jump Start Rails/training/app/assets/config /Users/mac/Documents/Projects/Web 开发/Ruby On Rails/快速启动 Rails/training/app/assets/images /Users/mac/Documents/Projects/Web 开发/Ruby On Rails/Jump Start Rails/training/app/assets/javascripts /Users/mac/Documents/Projects/Web 开发/Ruby On Rails/Jump Start Rails/培训/应用程序/资产/样式表 /Users/mac/Documents/Projects/Web 开发/Ruby On Rails/Jump Start Rails/培训/供应商/资产/javascripts /Users/mac/Documents/Projects/Web 开发/Ruby On Rails/Jump Start Rails/培训/供应商/资产/样式表 /Users/mac/.rvm/gems/ruby-2.3.0/gems/jquery-rails-4.2.1/vendor/assets/javascripts /Users/mac/.rvm/gems/ruby-2.3.0/gems/coffee-rails-4.2.1/lib/assets/javascripts /Users/mac/.rvm/gems/ruby-2.3.0/gems/actioncable-5.0.0.1/lib/assets/compiled /Users/mac/.rvm/gems/ruby-2.3.0/gems/turbolinks-source-5.0.0/lib/assets/javascripts

Extracted source (around line #5):


// @import "bootstrap-responsive";

@import "bootstrap";
@import "bootstrap-responsive";

// body 

Rails.root: /Users/mac/Documents/Projects/Web Development/Ruby On Rails/Jump Start Rails/培训

应用程序跟踪 |框架跟踪 |全跟踪 应用程序/资产/样式表/custom.css.scss:5 app/views/layouts/application.html.erb:7:in `_app_views_layouts_application_html_erb___3077022213198936014_70226475759060' 请求

参数:

切换会话转储切换环境转储响应

标题:

文件的 Git 存储库:https://github.com/ScarletMcLearn/training

“application.html.erb”文件的代码(如果需要):

<!DOCTYPE html>
<html>
  <head>
    <title>Training</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

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

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 any plugin's vendor/assets/javascripts directory 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. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery

// = require bootstrap-sprockets

//= require jquery_ujs
//= require turbolinks
//= require_tree .


//= require bootstrap

Gemfile 的内容:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'bootstrap-sass'


# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

我的“application.css”文件:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope.
 *
 *= require_tree .
 *= require_self
 */

我的“custom.css.scss”文件:

// @import "bootstrap";

// @import "bootstrap-responsive";

//=reqire_self

@import "bootstrap";
@import "bootstrap-responsive";

// body 
//     padding-top: 60px;
//     padding-bottom: 40px
// 
// .sidebar-nav 
//     padding: 9px 0;
// 


body 
    padding-top: 60px;
    padding-bottom: 40px;

.sidebar-nav 
    padding: 9px 0;

我的“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 any plugin's vendor/assets/javascripts directory 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. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery

// = require bootstrap-sprockets

//= require jquery_ujs
//= require turbolinks
//= require_tree .


//= require twitter/bootstrap

我的宝石文件:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'bootstrap-sass'

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

gem 'twitter-bootstrap-rails'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

【问题讨论】:

听起来很傻,但您是否尝试过停止并重新启动服务器? :p 不,我没有。但是我刚才试了一下,没有用。 :( @GhostRider 做一些更新。 你能上传一个repo到github吗?这听起来像是那些时髦的问题之一...... 我不知道。我是不是该?如果是,我该怎么做?关键字就可以了。我会自己做。 :p :D @ScarletMcLearn 这是一个指南:guides.railsgirls.com/github :) 【参考方案1】:

您是否将以下内容添加到您的 Gemfile 并在此之后运行 bundle install

gem 'bootstrap-sass', '~> 3.3.6'
gem 'sass-rails', '>= 3.2'

rails 使用sprockets 框架来预处理资产,包括样式表、javascript 和图像。在预处理过程中,遇到@import "bootstrap";,它会遍历资产路径目录,以便找到导入的引导目录或文件。根据您遇到的错误,sprockets 引擎似乎无法找到 bootstrap

确保您已将bootstrap-sass 包含在您的Gemfile 中并运行bundle install 以查看错误是否消失。

【讨论】:

我添加了这些。我编辑并添加了 Gemfile。我之前添加了这些并且还运行了捆绑安装,并得到了这个错误。建议。 看起来您在Gemfile 中定义了spring。确保使用bundle exec spring stop 停止它,以便在重新启动服务器之前对Gemfile 进行更改。 我在终端中运行了这个命令,也重新启动了我的服务器。它仍然是一样的。 @Dharam :(【参考方案2】:

这样做:

application.css.scss

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require_self
 */

application.css

/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require_tree .
*/

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 any plugin's vendor/assets/javascripts directory 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.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require turbolinks

按特定顺序!

Gemfile.rb

gem 'sass-rails', '~> 5.0'
gem 'twitter-bootstrap-rails'

确保您的 gemfile 中有这 2 个 gem,不属于任何组。然后运行bundle install

【讨论】:

我以前做过这些。也上传了代码。但还是同样的问题。 你试过删除吗?导入“引导程序”;导入“引导响应”;和 '// = 需要引导链轮' 是的。我已经做了。但没有变化。 :

以上是关于如何修复错误:Ruby on Rails 中的“Sass::SyntaxError in Posts#index”?的主要内容,如果未能解决你的问题,请参考以下文章

在Michael Hartl的Ruby on Rails教程中获取错误消息:MicropostsController中的NoMethodError #create,如何避免收到此消息?

Ruby on rails 中的捆绑错误

Ruby on rails 中的资源未找到错误

如何从 Ruby on Rails 中的字符串值创建变量名?

Ruby on Rails 中的后端管理

如何在 ruby​​ on rails 中发布数据