Cells - Components for Rails
Cells are view components for Rails. They are mini-controllers with their own MVC stack, can invoke logic and render views. They bring back OOP to Rails' view layer and make writing reusable portlets for your applications fun.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
$ rails g 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(opts)
@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/views/layouts/application.html.erb
<h1>Good buy!</h1>
<div id="sidebar">
<%= render_cell :shopping_cart, :display,
:orders => @orders %>
Render the cell in your views, or even in actions.
Of course, there is more. Cells can contain cells. Cells can do caching. Cells can be interactive. See more in our examples .
