Engine's Assets with Rails 3.1

Engine's assets with Rails 3.1

The paths to the all the engines' assets folders are automatically loaded.

The assets themselves are not loaded by default. This is understandable as the loading is done with require_tree ., which loads all css/js from the current folder (i.e. the main application assets' folder) but doesn't say anything about the engines assets.

The easy solution is to ask the user to require the js/css in application.js/css or wherever else it is needed. As the paths are loaded correctly, the user only need to specify the name of your asset (I'd recommend using the name of your engine). Example:

Appended to main_app/app/assets/javascripts/application.js:

//= require your_engine_name

If you have split your js in different files, your file your_engine_name/app/assets/javascripts/your_engine_name.js could have the following:

//= require_tree .

This will load all js files in your_engine_name/app/assets/javascripts/, as the "." refers to the local folder (in this case the folder of your engine's javascripts).

Note that ActionView::Helpers::AssetTagHelper.register_javascript_expansion appears not to have any effect when config.use_sprockets is set. I hope they'll at least put a warning in that case.

If you have a rake task to install your engine, then you could do the append to application.js.

Another way for the user to include it is to insert <%= javascript_include_tag "your_engine_name" %> in the erb layout.

I don't think there is a way to have it inserted automatically

Rails 3.1 Mountable Engines : Assets (Images) not copied to the host application?

Solved this issue.

It was as simple as do :

rake assets:precompile

That's it.

Rails 3 Engine & Static assets

For performance reason, static assets serving is disabled in production mode. Your webserver should be configured to serve theses assets.

Look at this discussion if your are using nginx as a webserver.

rails 3.1.1 engines - with mountable engines, is it possible to access parent app assets, default layout?

I managed to get this working with the following steps:

  1. In my parent app I was mounting the engine in routes.rb

    mount PluginName::Engine => '/plugin_name'

    I just removed it.

  2. Created an application controller as Ryan Bigg below had stated.

    class PluginName::ApplicationController < ApplicationController
    ...
    end
  3. As I wanted to have things name spaced when generating controllers, models, tests so you have to essentially comment out the isolate_namespace PluginName lib\plugin_name\engine.rb when I wanted the gem to be run in the parent app.

    It is not yet an ideal solution. off the top off my head, I could use something like:

    isolate_namespace PluginName if %w[development testing].include?(Rails.env)

    but will have to test if this is practical.

Kudos to Ryan for helping me find my way many thanks

Furthermore, the same can be done with the --mountable switch version and all you need to do is one further step in your engines config/routes.rb replace

PluginName::Engine.routes.draw do

with

Rails.application.routes.draw do

How do you get static assets in a Rails Engine to work in Rails 3.0 and Rails 3.1?

This question is a bit vague, but let me see if I can't give a decent answer. I'm not familiar with Rails 3.1 yet, but I understand if you want to separate your static assets you can create a gem to house your assets and just plug them in like that. Checkout

http://guides.rubyonrails.org/asset_pipeline.html

http://railscasts.com/episodes/279-understanding-the-asset-pipeline

After that, I have more experience implementing engines in rails 3.0, which can be a bit crazy. Engines sort of work as gemified applications that can plug into other application and meshes all of its code into the main app, this includes assets, models, controllers, views, routes, etc. Which can be both a blessing and a curse. It's certainly unique in that you can create divisions between applications and be great for separating and reusing content, but it's a curse if you don't properly namespace things and cause headaches with name collisions.

I don't know exactly how you SHOULD bundle your assets, but if it were me I'd just use the the engine as the container for static assets if it pertains to just that engine. And at least in rails 3.0 you have to do it that way. Well or you could use Jammit, http://documentcloud.github.com/jammit/

Hope that's what you were looking for.

Rails 3.1 engine not loading vendor assets

In your gemspec file - it doesnt look like you include files from the vendor directory, guessing that is your main issue, check asset path after adding and reinstalling - if you look in your gem repository now, i am guessing the files in vendor are not included

Also i would move twitter_bootstrap.js into the twiiter_bootstrap directory and rename it index.js, not sure if this is required or not (you will need to update paths in that file also)

You might also be interested in this post? http://house9.blogspot.com/2011/06/rails-31-asset-gem.html

Rails 3.1 - Accessing Parent Application Models from Mountable Engine

By design, Mountable Engines are isolated from their parent. However, you can in theory expose portions of the parent through a REST API using ActiveResource or the like. You can also use a 'Full Engine' which acts as a sort of plugin rather than a Mountable one - Rails 3.1: Engine vs. Mountable App



Related Topics



Leave a reply



Submit