【问题标题】:How to use 'SASS' from html template on a rails app?如何在 Rails 应用程序上使用 html 模板中的“SASS”?
【发布时间】:2016-11-04 11:55:59
【问题描述】:

我正在尝试在我的 rails 应用程序中安装一个 html 模板,因此我移动了 public 文件夹中的所有资产并重命名了 erb 中的所有 html 文件。

一切正常,除了“sass”不起作用,html 模板的 SASS 文件夹中的任何文件都不起作用。

那么如何在我的 rails 应用上使用 html 模板的 SASS 文件呢?

【问题讨论】:

    标签: html ruby-on-rails ruby sass


    【解决方案1】:

    那么我怎样才能使用我的 html 模板的 SASS 文件呢? 轨道应用?

    1) 如果您使用的是bundle install,则在您的 Gemfile 中安装 SASS Gem gem 'sass',或者通过命令行:gem install sass

    2) 在您的 SASS 文件夹中创建一个名为“manifest.txt”的文件,例如 public/src/sass/manifest.txt

    3) 在manifest.txt 中,按照包含顺序列出您希望合并到单个 CSS 文件中的每个文件,例如:

    framework.sass
    framework_overides.sass
    homepage.sass
    contact.sass
    global.sass
    

    4) 打开你的Rakefile 并创建这个 rake 任务:

    require 'sass'
    desc "Parse SASS manifest.txt and convert to single CSS file"
    task :sass do
      # your template directory
      src_dir = File.join Rails.root, 'public/src'
    
      # SASS directory
      sass_dir = File.join src_dir, "sass"
    
      # name of the single output file
      app_css_file = File.join src_dir, "css/application.css"
    
      # parse SASS manifest
      sass_manifest = IO.readlines File.join(sass_dir, "manifest.txt")
    
      # clear previous app_css_file, without having to require 'fileutils'
      File.open(app_css_file, 'w') {|f| f.write ""}
    
      # write to app_css_file
      File.open(app_css_file, 'a+') do |f|
        sass_manifest.each do |x|
    
          # remove code comments
          next if x.strip =~ /^#/
    
          # parse extension
          case syntax = File.extname(x.strip).delete('.').downcase
    
          # convert and append scss
          when /scss/ then
            f.write Sass::Engine.new(IO.read(File.join(sass_dir, x.strip)), :syntax => :scss, :cache => false).render
    
          # convert and append sass
          when /sass/ then
            f.write Sass::Engine.new(IO.read(File.join(sass_dir, x.strip)), :cache => false).render
          end
        end
      end
    end
    

    5) 使用此命令:rake sass

    【讨论】:

    • Sass::SyntaxError: Invalid @import: 预期的行尾,是“;”。 (萨斯):1
    • @Ghamtre 你的.sass 文件中不能有; 分号。
    • @Ghamtre 你在用SCSS吗?
    • 是的:|我正在使用 SCSS
    • @Ghamtre 不用担心。给我五分钟,我将有一个更新的答案,适用于 .scss 文件
    【解决方案2】:

    对于引导模板,我也遇到了同样的问题。所以我分享一下我的经验,希望对你有帮助。 我所做的是我删除了这个宝石(如果你正在使用它)

    gem 'bootstrap-sass'
    

    并替换为这两个宝石,

    gem 'therubyracer'
    gem 'less-rails-bootstrap'
    

    您还需要根据其官方docapplication.jsapplication.css中初始化它们。

    看看这个blog。它逐步介绍了如何将自定义引导模板集成到 Rails 中。它可能会帮助你。

    【讨论】:

    • 感谢您的帮助,但现在我遇到了这个错误:ActionView::Template::Error (undefined method `gsub' for nil:NilClass):
    • 很难从一行中看出,你应该在错误行之前使用一些调试工具,如byebugpry来找出问题所在。
    猜你喜欢
    • 2013-10-20
    • 1970-01-01
    • 2011-04-08
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 2012-04-11
    相关资源
    最近更新 更多