1 min read

Rails 3.2 Engines with CSS injection

The Ruby on Rails framework has a lot of benefits but not an easy component reuse among different projects.

The Rails Engines are becoming more and more popular to organize the code, but are usually used to share applications with a full layout and not just features you can embed easly in your current application, like navigation bar, pages, contact form,..

With Rails 3.2 and a smart structure is possible to mantain different engines in different applications in parallel, the unique problem seems that Coffeescript and sCSS are not supported in assets pipeline.

In the egde guides there is a quite in deep Engines usage analisys, but a missing feature for me was that main_app layout isn't inherited by engine controller layout out of the box.
An hacky but working way to extend the common layout in the engine is to hook it in the main_app application.html.erb

<%= stylesheet_link_tag    request.env["action_dispatch.routes"].routes.routes[0].defaults[:controller], :media => "all" %>

So finally You can override the background of you preferred engine sets in the main_app and ( and maintain) in a painless way a moltitude of webapps with different styles and data but a common feature set.

Here is an example application to look at. If You know better ways to embed Rails engines with css and assets, let know!

Credits 
Pivotal Labs
Stackoverflow