【问题标题】:Rails 3.1 Generating Stylesheets attached to ActiveRecord Model objectsRails 3.1 生成附加到 ActiveRecord 模型对象的样式表
【发布时间】:2012-05-11 10:02:03
【问题描述】:

我们过去在 Rails 3.0 中所做的是让回形针将一个文件附加到我们的“站点”模型中,该模型代表 css 样式表。我们的应用程序中有大量站点,每个站点都有一个样式表,每次站点中的属性(如 site.color1 或 site.color 2 更改)时都会生成该样式表。 然后我们有一个 .sass 文件,它使用像 $color1 这样的变量,这些变量被解释为生成正确的 css 文件并用回形针上传

这是 Site.rb(模型)的相关部分:

has_attached_file :stylesheet, PAPERCLIP_OPTIONS

def generate_site_specific_stylesheet(force = false)
  if use_custom_stylesheet && (force || color_changed?)
      stylesheets_path = Rails.root + 'app/assets/stylesheets'
      template = File.read(stylesheets_path + '_site_specific_stylesheet.sass')
      sass_engine = Sass::Engine.new(template, :load_paths =>[stylesheets_path.to_s, '.'],
                                 :template_location => stylesheets_path.to_s, :custom =>            {:site => self})
      stylesheet_contents = sass_engine.render
      puts stylesheet_contents
      self.stylesheet = StringIO.new stylesheet_contents
      self.stylesheet.instance_write(:file_name, 'site_stylesheet.css')
      self.stylesheet.instance_write(:content_type, 'text/css')
  end
  true
end

正确的问题是,当这些样式表被创建时,它们无法识别像 image-path 这样的 sass 助手。我无法找到解决方法。我们确实在模块(模块 Sass::Script::Functions)中定义了自定义 sass 函数,但是助手也不能在那里工作,我不知道如何在 include ActionView::Helpers 中使用 image_path 方法,尽管我尝试将该模块包含到我的 sass 函数模块中,但没有成功。

有人对此有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 paperclip asset-pipeline assets


    【解决方案1】:

    image-path helper 不隶属于 SASS。这是一种方法,provided 我的 Rails 本身。尝试在模型类中包含以下模块:

    include ActionView::Helpers::AssetTagHelper
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 2011-10-18
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      相关资源
      最近更新 更多