替换rails scaffold样式表内容

时间:2014-02-13 21:18:03

标签: html css ruby-on-rails ruby scaffold

您好我已为支架生成器制作了一些自定义模板视图并将其放入

lib/templates/erb/scaffold/_form.html.erb
lib/templates/erb/scaffold/edit.html.erb
lib/templates/erb/scaffold/index.html.erb
lib/templates/erb/scaffold/new.html.erb
lib/templates/erb/scaffold/show.html.erb

到目前为止一切都很好,现在我想用scaffolds.css.scss(由脚手架指令生成)文件替换一些自定义样式的内容。

我已经从上面的行中包含了scaffolds.css.scss或scaffolds.css,没有任何反应。

我已经阅读了这个http://guides.rubyonrails.org/generators.html,但是只提到了如何跳过css生成器,而不是如何替换它

========编辑=========

调试我发现原始的脚手架文件来自

/home/jgutierrez/.rvm/gems/ruby-1.9.3-p484/gems/railties-3.2.13/lib/rails/generators/rails/scaffold/templates/scaffold.css

[6, 15] in /home/jgutierrez/.rvm/gems/ruby-1.9.3-p484/gems/sass-rails-3.2.4/lib/rails/generators/sass_scaffold.rb

   6      class ScaffoldBase < ::Rails::Generators::NamedBase
   7        def copy_stylesheet
   8          dir = ::Rails::Generators::ScaffoldGenerator.source_root
   9          file = File.join(dir, "scaffold.css")
   10          debugger
=> 11          puts    "-------------------------------------------------#{file.path}"
   12          converted_contents = ::Sass::CSS.new(File.read(file)).render(syntax)
   13          create_file "app/assets/stylesheets/scaffolds.css.#{syntax}", converted_contents
   14        end
   15      end

无论如何更换拍摄地点的地方?喜欢模板视图的替换方式吗?

2 个答案:

答案 0 :(得分:1)

我不确定,但我认为没有记录,因为生成器总是在同一个地方复制相同的样式表。所以一旦你的总体布局有点固定,你最好总是跳过这个,因为scaffold.css.scss大部分时间都会弄乱css的其余部分:)

不确定这是否有效,但根据code,您需要调用scaffold.css文件。

答案 1 :(得分:1)

最近我遇到了类似的问题,谷歌把我带到了这里。我想要的是替换生成的视图的默认scss。最后我解决了这个问题,所以我想为稍后来这里的人分享解决方法。我的环境是带有Rails 5.0的Ruby 2.3.0,我认为这至少也适用于Rails 4+。

作为JGutierrezC的调试, .scss 文件是由sass-rails gem生成的,而不是原始的railties gem。如果您查看sass-rails的代码,您会发现它只是在../template下找到该文件:

https://github.com/rails/sass-rails/blob/5-0-stable/lib/rails/generators/scss/assets/assets_generator.rb#L6

require "rails/generators/named_base"

module Scss
  module Generators
    class AssetsGenerator < ::Rails::Generators::NamedBase
      source_root File.expand_path("../templates", __FILE__)

      def copy_scss
        template "stylesheet.scss", File.join('app/assets/stylesheets', class_path, "#{file_name}.scss")
      end
    end
  end
end

因此,如果您只在模板目录下放置 .css ,它将无法工作,因为生成器始终搜索目录../template中的模板文件,该文件不遵循转换在导轨指南中描述。那么,我们需要做的是覆盖这种行为。让我们进入rails的主生成器代码:

https://github.com/rails/rails/blob/5-0-stable/railties/lib/rails/generators.rb#L354

def self.lookup(namespaces) #:nodoc:
  paths = namespaces_to_paths(namespaces)

  paths.each do |raw_path|
    ["rails/generators", "generators"].each do |base|
      path = "#{base}/#{raw_path}_generator"

      begin
        require path
        return
      rescue LoadError => e
        raise unless e.message =~ /#{Regexp.escape(path)}$/
      rescue Exception => e
        warn "[WARNING] Could not load generator #{path.inspect}. Error: #{e.message}.\n#{e.backtrace.join("\n")}"
      end
    end
  end
end

在分配path后插入一行以打印它并调用rails g scaffold ...时,您可能会得到以下内容:

...


"rails/generators/rails/scss/scss_generator"
"generators/rails/scss/scss_generator"
"rails/generators/rails/scss_generator"
"generators/rails/scss_generator"
"rails/generators/scss/assets/assets_generator"
      invoke    scss

...

这些行显示了rails的生成器查找过程。 is-resuce 块执行一项操作 - 按包含生成器并要求它的订单查找第一个目录,然后返回。因此,如果我们在最后一个目录之前在目录中创建我们的生成器,它可能需要rails而不是原始的。只需将原始文件复制到您的应用程序工作区:

(your ruby install path)/ruby-2.3.0/gems/sass-rails-5.0.5/lib/rails/generators/scss/assets/*

(your app path)/lib/generators/rails/scss/

并将assets_generator.rb重命名为scss_generator.rb

然后再次运行scaffold命令,你会发现输出不同:

...

"rails/generators/rails/scss/scss_generator"
"generators/rails/scss/scss_generator"
      invoke    scss

...

Rails找到我们创建的生成器并且需要它,很好!现在scss_generator将使用模板(your app path)/lib/generators/rails/scss/template/stylesheet.scss。所以最后要做的就是自定义它,并且更改将反映到脚手架的视图scss。

如果您想替换默认的 scaffold.scss ,则会有更多的事情。您需要将相关方法复制到scss_generator.rb,因为您已更改了原始查找路径,并且rails不会将它们复制到原始文件中。

require "rails/generators/named_base"

module Scss
  module Generators
    class AssetsGenerator < ::Rails::Generators::NamedBase
      source_root File.expand_path("../templates", __FILE__)

      def copy_scss
        template "stylesheet.scss", File.join('app/assets/stylesheets', class_path, "#{file_name}.scss")
      end

    end
  end
end

# ruby-2.3.0/gems/sass-rails-5.0.5/lib/rails/generators/scss/scaffold/scaffold_generator.rb
require "sass/css"
module Sass
  module Generators
    class ScaffoldBase < ::Rails::Generators::NamedBase
      def copy_stylesheet
        dir = "#{::Rails.application.config.root}/lib/templates/erb/scaffold/"

        file = File.join(dir, "scaffold.css")
        converted_contents = ::Sass::CSS.new(File.read(file)).render(syntax)
        create_file "app/assets/stylesheets/scaffolds.#{syntax}", converted_contents
      end
    end
  end
end


# ruby-2.3.0/gems/sass-rails-5.0.5/lib/rails/generators/scss/scaffold/scaffold_generator.rb
module Scss
  module Generators
    class ScaffoldGenerator < ::Sass::Generators::ScaffoldBase
      def syntax() :scss end
    end
  end
end

并将copy_stylesheet方法中的变量dir的值更改为您放置scaffold.scss的位置,例如:

dir = "#{::Rails.application.config.root}/lib/templates/erb/scaffold/"

现在您可以根据自己的意愿使用自己的scaffold.css!