简体   繁体   English

为共享样式表创建gem并在Rails应用程序中使用它

[英]Creating a gem for shared stylesheets and using it in a Rails application

I'm trying to create a gem to put all my common stylesheets in. I can't to figure out how to import the stylesheets in a Rails application. 我正在尝试创建一个gem来放置所有常见的样式表。我无法弄清楚如何在Rails应用程序中导入样式表。 Here's what I've done so far (following this guide ): 这是我到目前为止所做的事情(遵循本指南 ):

  1. I ran bundle gem apple_core . 我运行了bundle gem apple_core

  2. I modified apple_core.rb to contain a rails engine class like so: 我修改了apple_core.rb以包含一个rails引擎类,如下所示:

     require "apple_core/version" module AppleCore class Engine < Rails::Engine end end 
  3. I created the apple_core stylesheets directory using mkdir -p lib/apple_core/app/assets/stylesheets/apple_core . 我使用mkdir -p lib/apple_core/app/assets/stylesheets/apple_core创建了apple_core stylesheets目录。

  4. I created an index file inside the apple_core stylesheets directory and added the following to it: 我在apple_core stylesheets目录中创建了一个索引文件,并在其中添加了以下内容:

     /* *= require_self *= require_tree . */ 
  5. I made a test.css.scss file in the same directory and added: 我在同一目录下创建了一个test.css.scss文件,并添加了:

     a { color: red; } 
  6. I created a GitHub repository for the gem and pushed it. 我为gem创建了一个GitHub存储库并推送它。

  7. In my Rails application, I added the gem to my Gemfile in the assets group. 在我的Rails应用程序中,我将gem添加到资产组中的Gemfile中。

  8. I ran bundle install . 我运行了bundle install

I tried to include my gem in one of my files using @import "apple_core"; 我尝试使用@import "apple_core";将我的gem包含在我的一个文件中@import "apple_core"; , but when I attempted to load the page I received a couldn't find file 'apple_core' error. ,但是当我试图加载页面时,我收到了一个couldn't find file 'apple_core'错误。 What am I missing here? 我在这里错过了什么?

You just have to mirror the directories in your gem as they are in a rails project. 您只需镜像gem中的目录,就像它们在rails项目中一样。 So you can put assets to app/assets , lib/assets and vendor/assets . 因此,您可以将资产放入app/assetslib/assetsvendor/assets You then have to make sure that these files are included in your gemspec, for example: 然后,您必须确保这些文件包含在您的gemspec中,例如:

  s.files = Dir["{app,config,db,lib,vendor}/**/*"] + ["MIT-LICENSE", "Rakefile", "Readme.md"]

So the Path should then be vendor/assets/stylesheets/apple_core . 因此Path应该是vendor/assets/stylesheets/apple_core

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

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