Rails 皮肤找不到 SASS 变量

Rails Skins not Finding SASS Variables

提问人:Liz 提问时间:8/3/2023 更新时间:8/7/2023 访问量:56

问:

我正在尝试在我的 Rails 应用程序中实现用户选择的皮肤,正如这篇 SO 帖子中所建议的那样。

我把我的皮肤放在一个文件夹中,每个皮肤的结构都是这样的:app/assets/stylesheets/skins

$color-background: #F0E6DA !important;       /* Main background */
$color-background-alt: #a8a198 !important;   /* Alternate background */
$color-background-box: #ffffff !important;   /* Box Background */

$color-text: #000000 !important;             /* Main text (contrast bg) */
$color-text-alt: #653D3D !important;         /* Alternate text */
$color-text-box: #000000 !important;         /* Box text (contrast box-bg) */

$color-admin: #225e51 !important;            /* CTAs & accents (main/opp txt ok) */
$color-accent-main: #842a57 !important;      /* Good with main/opp text */
$color-accent-subtle: #a86989 !important;    /* Subtle (main/opp text) */

$color-bad: #983C3C !important;              /* both text */
$color-good: #16537e !important;             /* both text */

我的 SCSS 的其余部分在我的文件中,其中 CSS 引用了这些变量名称。application.bootstrap.scss

我在我的例子中引用了这个:application.html.erb

<%= stylesheet_link_tag "#{@current_theme}", "data-turbo-track": "reload" %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

并把这个放在我的:application_helper.rb

module ApplicationHelper

    def current_theme
        if @current_user && @current_user.chosen_skin != ( nil || "" )
            @current_theme = @current_user.chosen_skin
        else
            @current_theme = 'skins/default'
        end
    end
    
end

我将以下内容添加到我的(并重新启动了我的服务器):assets.rb

Rails.application.config.assets.precompile += %w( skins/* )

当我尝试加载页面时,我收到一个错误,指出我第一次引用其中一个文件中的变量。(是的,我仔细检查了我是否确实有一个叫做.Error: Undefined Variable.application.bootstrap.scssskinsdefault.scss.css

当我查看应用程序标头中呈现的行时,它如下所示:

<link rel="stylesheet" href="" data-turbo-track="reload" />
<link rel="stylesheet" href="/assets/application-3484df6482827f75364ce1ebdc6829c5125776d4a06144b1a5c7a9fe2e71f7b9.css" data-turbo-track="reload" />

谁能看到我在这里哪里出了问题?

CSS Ruby-on-Rails sass

评论

0赞 Ghulam Farid 8/6/2023
你看到以下内容了吗?stackoverflow.com/questions/21911620/......您也可以尝试将Rails.application.config.assets.precompile += %w( skins/* )Rails.application.config.assets.precompile += %w( skins/*.scss )
0赞 Eliezer Berlin 8/11/2023
我不熟悉 Ruby On Rails,但听起来它没有被导入到你的 ,而是被视为一个单独的文件。通常,SCSS 文件只会从文件本身加载 ed 或 d 的变量,并且由于它不是同一个文件,因此可能不会加载变量。(SCSS 不是 javascript,你不能从 HTML 中的上一个文件加载变量,你必须将文件直接导入到你想使用的 SCSS 文件中)<%= stylesheet_link_tag "#{@current_theme}", "data-turbo-track": "reload" %>application.bootstrap.scss@import@use
0赞 Eliezer Berlin 8/11/2023
在您在此处链接的帖子中: stackoverflow.com/questions/11928476/themes-in-rails-app ,人们正在创建一个主题文件,其中包含所有 CSS,该文件是从从 main.scss 文件导入的@mixin创建的。(也就是说,主文件实际上是定义所有变量的“theme.scss”,然后在每个主题文件的底部,它导入“main.scss”以使用主题中定义的变量。
0赞 Eliezer Berlin 8/11/2023
也就是说,他们删除了这一行,并且只使用主题文件。在主题文件中,导入 application.scss 文件。<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

答:

0赞 Zoran Majstorovic 8/7/2023 #1

尝试更正:application_helper.rb

module ApplicationHelper

  def current_theme
    if @current_user && @current_user.chosen_skin.present?
      @current_theme = @current_user.chosen_skin
    else
      @current_theme = 'skins/default'
    end
  end

end