简体   繁体   English

Rails4资产预编译失败

[英]Rails4 asset precompiling fails

In my application, I have various themes user can select from for a particular web page. 在我的应用程序中,我可以为特定网页选择各种主题。 These themes just differs in terms of color and font types only. 这些主题仅在颜色和字体类型方面有所不同。 So in order to not to repeat the css declaration, I have created a scss file containing common css declarations ztheme.css.scss that contains common css styles in all themes. 因此,为了不重复CSS声明,我创建了一个包含通用CSS声明ztheme.css.scss的scss文件,该声明在所有主题中均包含通用css样式。 And I have various themes scss files (eg silver.css.scss , melon.css.scss - shown below) with merely variables value defined for color and font type. 而且我有各种主题的scss文件(例如, silver.css.scssmelon.css.scss如下所示),其中只为颜色和字体类型定义了变量值。 In all these theme scss files, you can see at the bottom it imports the common ztheme.css.scss as @import 'ztheme' . 在所有这些主题scss文件中,您可以在底部看到将常见的ztheme.css.scss@import 'ztheme' So that whene these themes files get compiled, they will have common css styling with appropriate color and font types value. 这样,当这些主题文件被编译时,它们将具有通用的CSS样式以及适当的颜色和字体类型值。

silver.css.scss

$nav_bg_color: #F6F6F6;
$page_font: 'Sintony', sans-serif;
$page_txt_color: #000033;
@import 'ztheme';

melon.css.scss

$nav_bg_color: #F2F6C6;
$page_font: 'Arial', verdana;
$page_txt_color: #628002;
@import 'ztheme';

ztheme.css.scss

body{
  padding-bottom: 20px;
  background: $nav_bg_color;
  font-family: $page_font;
  text-rendering: optimizelegibility;
  color: $page_txt_color;
}

The problem is when I deploy this code on heroku and asset precompiling kicks off and then it fails in ztheme.css.scss because it looks for the value of variables defined in it. 问题是当我在heroku上部署此代码并启动资产预编译,然后在ztheme.css.scss失败,因为它会在其中查找定义的变量的值。 and throw this error: 并抛出此错误:

remote:        rake aborted!
remote:        Sass::SyntaxError: Undefined variable: "$nav_bg_color".
remote:        /tmp/build_ded002611a0752bd96038a671fd8051c/app/assets/stylesheets/themes/ztheme.css.scss:9

However you can see, ztheme is being imported in every theme files, so when ztheme will be imported in those files, these zthemes' value should get populated properly and it should not throw Undefined variable error. 但是,您可以看到, ztheme已导入每个主题文件中,因此当ztheme将导入这些文件中时,这些zthemes的值应正确填充,并且不应引发Undefined variable错误。

How to solve this problem? 如何解决这个问题呢?

I have structured my assets as follows: 我的资产结构如下: 在此处输入图片说明

It's difficult to determine what code is in what file from your code, but I think you may have done your @imports backwards. 很难从代码中确定文件中的代码,但是我认为您可能已经将@imports反向执行了。 Try importing all of your themes into 'ztheme.css.scss', because they're where you define '$nav-bg-color'. 尝试将所有主题导入“ ztheme.css.scss”,因为它们是定义“ $ nav-bg-color”的位置。

This might not be an all-inclusive solution, but somewhere to start from. 这可能不是一个全面的解决方案,而是一个起点。

I hope this helps in some way. 我希望这会有所帮助。

After several attempts, finally I found the solution. 经过几次尝试,终于找到了解决方案。 I had to add a default value for these variables in ztheme.css.scss that gets overridden in theme specific scss 我必须在ztheme.css.scss中为这些变量添加一个default值,该default值会在主题特定的scss中被覆盖

$nav_bg_color: #F1C5CE  !default;
$page_font: Arial, Verdana !default;
$page_txt_color: #459823 !default;

body{
  padding-bottom: 20px;
  background: $nav_bg_color;
  font-family: $page_font;
  text-rendering: optimizelegibility;
  color: $page_txt_color;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM