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. Here's what I've done so far (following this guide):
I ran bundle gem apple_core
.
I modified apple_core.rb to contain a rails engine class like so:
require "apple_core/version"
module AppleCore
class Engine < Rails::Engine
end
end
I created the apple_core stylesheets directory using mkdir -p lib/apple_core/app/assets/stylesheets/apple_core
.
I created an index file inside the apple_core stylesheets directory and added the following to it:
/*
*= require_self
*= require_tree .
*/
I made a test.css.scss file in the same directory and added:
a
{
color: red;
}
I created a GitHub repository for the gem and pushed it.
In my Rails application, I added the gem to my Gemfile in the assets group.
I ran bundle install
.
I tried to include my gem in one of my files using @import "apple_core";
, but when I attempted to load the page I received a couldn't find file 'apple_core'
error. What am I missing here?
You just have to mirror the directories in your gem as they are in a rails project. So you can put assets to app/assets
, lib/assets
and vendor/assets
. You then have to make sure that these files are included in your gemspec, for example:
s.files = Dir["{app,config,db,lib,vendor}/**/*"] + ["MIT-LICENSE", "Rakefile", "Readme.md"]
So the Path should then be vendor/assets/stylesheets/apple_core
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With