Cells - Components for Rails
- So, you're looking for a way to render actions inside actions?
- You're sick of a non-functional #render_component ?
- You've lost track of your helpers and partials, and how they fit together with controllers and views?
Well, Cells is the building block for building blocks in Rails.
Cells look like controllers. They are super fast. They provide clean encapsulation. They are reuseable across your project. They can be plugged anywhere in your views or controllers.
Call them "mini controllers" . But hey, they are faster!
Call them "partials on steroids" . But wait, they are object-oriented instances, not a loose helper+partial thing.
Show me!
You need something like a shopping cart, which appears on almost every page of your app.
You wouldn't use a partial and a helper, would you?
Generate
$ script/generate cell ShoppingCart display create app/cells/ create app/cells/shopping_cart create app/cells/shopping_cart_cell.rb create app/cells/shopping_cart/display.html.erb
The generator provides you with all the files you need.
Implement
File: app/cells/shopping_cart_cell.rb
class ShoppingCartCell < Cell::Base
def display
@orders = @opts[:orders]
render
end
end
Implement the cell state.
Layout
File: app/cells/shopping_cart/display.html.erb
<div id="cart"> You have <%= @orders.size %> in your shopping cart. </div>
Setup a view for the cell state, as you know it from controller actions.
Plug in!
File: app/controllers/products_controller.rb
def show @cart = render_cell :shopping_cart, :display, :orders => @orders render end
Render the cell in your controller actions, or even in views.
Of course, there is more. Cells can contain cells. Cells can do caching. Cells can be interactive. See more in the example app .