提问人:Liz 提问时间:8/3/2023 更新时间:8/7/2023 访问量:56
Rails 皮肤找不到 SASS 变量
Rails Skins not Finding SASS Variables
问:
我正在尝试在我的 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.scss
skins
default.scss.css
当我查看应用程序标头中呈现的行时,它如下所示:
<link rel="stylesheet" href="" data-turbo-track="reload" />
<link rel="stylesheet" href="/assets/application-3484df6482827f75364ce1ebdc6829c5125776d4a06144b1a5c7a9fe2e71f7b9.css" 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
评论
Rails.application.config.assets.precompile += %w( skins/* )
Rails.application.config.assets.precompile += %w( skins/*.scss )
<%= stylesheet_link_tag "#{@current_theme}", "data-turbo-track": "reload" %>
application.bootstrap.scss
@import
@use
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>