Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add asset path in rails mountable engine?

How can I add vendor/assets/javascripts/mymountableengine or vendor/assets/stylesheets/mymountableengine to my mountable engine's assets paths? I want to be able to require files from these folders in my mountable engine's application.js/application.css file with sprockets.

Rails 3.2.2

Thanks.

like image 266
Yeggeps Avatar asked Apr 19 '12 08:04

Yeggeps


People also ask

What are assets in Rails?

Generally asset is anything that browser loads after it gets the HTML page. Meaning javascript, css and any images. But as you pointed out there are two different image types in a rails project.

How do you Precompile an asset?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What is Mount in Rails routes?

Mount within the Rails routes does the equivalent of a Unix mount . It actually tells the app that another application (usually a Rack application) exists on that location. It is used mostly for Rails Engines.


2 Answers

Turns out they were already loaded! Just put them in the wrong directory: engine/vendor/assets/javascripts/engine - putting them in engine/vendor/assets/javascripts made them requireable. For others, just check Rails.application.config.assets.paths to see which paths are loaded, I believe engines use the parents Sprockets environment, so to add paths just use Rails.application.config.assets.paths << "path/here"

like image 95
Yeggeps Avatar answered Oct 12 '22 22:10

Yeggeps


I do like this:

module MyEngine
  class Engine < ::Rails::Engine

    config.assets.paths << File.expand_path("../../assets/stylesheets", __FILE__)
    config.assets.paths << File.expand_path("../../assets/javascripts", __FILE__)
    config.assets.precompile += %w( my_engine.css )

  end
end
like image 20
Pavel Evstigneev Avatar answered Oct 13 '22 00:10

Pavel Evstigneev