【问题标题】:Getting error on CSS with Ruby On Rails使用 Ruby On Rails 在 CSS 上出现错误
【发布时间】:2015-12-21 02:39:26
【问题描述】:

当我运行它时,我得到了错误: Sass::SyntaxError in Welcome#index 错误 index.html.erb:

这些是我的 ruby​​ 文件:

<!DOCTYPE html>
<html>
    <head>
        <%= stylesheet_link_tag "welcome.scss" %>
    </head>

    <body>
        <div id = "header">
            <p id = "title">Welcome To Dot</p>
        </div>
        <h1>Make Life Easier</h1>
        <h2>Enter</h2>
    </body>
</html>

Welcome.scss:

#title{
    font-size:30px;
    font-weight: bold;
    font-family: fantasy;
    color: #0b1e40;
    text-align: left;
}

#header{
    background-color: sandybrown;
    position: fixed;
    z-index: 1;    
}

application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>Dot</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

宝石:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# 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.1.0'
# 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 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', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

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

# 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'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
end

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

welcome_controller.rb

class WelcomeController < ApplicationController

    def index
    end
end

routes.rb

Rails.application.routes.draw do

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'welcome#index'
end

我不确定我为什么会得到这个。如果我将“应用程序”改回“默认”,我将不再收到错误,但 CSS 不会显示在网页上。这是我的第一个 Ruby On Rails 项目,如果有任何帮助,我将不胜感激。

欢迎#index 中的 ExecJS::ProgramError 显示 C:/Users/Michael/Projects/dot/app/views/layouts/application.html.erb 其中第 5 行提出:

TypeError: 对象不支持此属性或方法 Rails.root: C:/Users/Michael/Dropbox/Docs/Homework/Projects/dot

app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__35629208_74953452'

【问题讨论】:

  • 您是否安装了正确的 gem 以在 Rails 中使用 SCSS/SASS?您需要安装 gem 'sass'gem 'sass-rails' 才能使 SASS 正常工作。
  • 你在标题末尾缺少一个分号 color 值。
  • 是的,我找到了分号,但我没有安装这些 gem。我现在正在这样做,所以希望这就是问题所在!
  • SASS 是 SCSS 文件的编译器,它们将被编译并输出为普通的 CSS 文件,然后使用,因为浏览器不理解 SCSS。但是对于开发人员来说,使用 SASS 语法更容易,因为 @extendmixins 等等......
  • @AlexanderTrust 我安装了这些 gem,但我仍然收到此错误:ExecJS::ProgramError in Welcome#index

标签: ruby-on-rails


【解决方案1】:

缺少分号(如果您曾经使用过 PHP,这将是您首先要寻找的东西之一):

#title{
  color: #0b1e40;
}

环顾四周,您遇到的错误似乎是这样的:

对象不支持此属性或方法 Rails.root

这表明您的路线存在问题:

#config/routes.rb
root "welcome#index" #-> you don't need (get "welcome#index")

--

您可能还对ExecJS(在您的系统上运行javascript 的类)有疑问——如果您使用的是Windows,您应该download nodeJS,安装并运行它,同时重新启动您的rails 服务器.

这应该可以解决在您的布局中调用 applicationdefault 的问题。

【讨论】:

  • 是的! node.js 是问题所在。我希望 Ruby 在他们的教程中让你知道这一点。谢谢!
【解决方案2】:

您的welcome.scss 中有语法错误 你忘了;

#title{
  ...
  color: #0b1e40;
  ... 
}

【讨论】:

    【解决方案3】:

    也许这对你有帮助......(如果你在 Windows 上使用 Rails)

    如何修复错误

    导航到 \app\views\layouts\application.html.erb

    从第 6 行更改

    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    

    <%= javascript_include_tag 'defaults', 'data-turbolinks-track' => true %>
    

    你就完成了!

    我在下一个网址找到了它

    http://mech.xyz/how-to-fix-ruby-on-rails-turbolinks-js-coffee-error-windows/

    【讨论】:

      【解决方案4】:

      使用资产管道。只需将 Welcome.scss 放入 app/assets/stylesheets 即可。删除&lt;%= stylesheet_link_tag "welcome.scss" %&gt;

      【讨论】:

      • 谢谢我删除了那行,但我仍然收到这个错误:ExecJS::ProgramError in Welcome#index Showing C:/Users/Michael/Projects/dot/app/views/layouts/application.html .erb where line #5 raise: TypeError: Object doesn't support this property or method
      • 你的application.html.erb的第5行写了什么?
      • @Mike 你的 gemfile 中有gem 'turbolinks' 吗?
      • @AlexanderTrust 这是第 5 行: true %>
      • @nobilik 我确实有 turbolinks,我将我的 gemfile 添加到问题中以便您查看。
      猜你喜欢
      • 2017-09-01
      • 2017-02-23
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多