Include gems in Rails 4

I created a gem (not an engine) that has several js files:

lib/assets/javascripts/mygem.js

      

In my application, I want to include this js in application.js:

# app/assets/javascripts/application.js

// require mygem  # DOESN'T WORK

      

This does not work.

Can gem assets be included? Or should I write a gem generator that will copy all the assets from the gem to the app's resource folder.

+3


source to share


1 answer


Add a class of class to the gem and claim it in the gem:

# lib/mygem/rails/engine.rb
module Bootstrap3AutocompleteInput
  module Rails
    class Engine < ::Rails::Engine

    end
  end
end

      

# lib/mygem.rb:
module Bootstrap3AutocompleteInput
  require 'mygem/rails/engine'

end

      

Gem assets are now available in the app:

Gemfile:



 gem 'mygem'

      

Application / Assets / JavaScripts / application.js

//= require mygem

      

Read about adding assets to your gems:

http://guides.rubyonrails.org/asset_pipeline.html#adding-assets-to-your-gems

+4


source







All Articles