A ViewModel layer for Ruby On Rails
Add viewmodel to your Rails application
bundle add viewmodel
Create a ViewModel object in a corresponding view_models
folder
app/
├─ view_models/
│ └─ products/
│ └─ show_view_model.rb
├─ views/
│ └─ products/
│ └─ show.html.erb
The contents of the view model look something like this:
class Products::ShowViewModel < ViewModel
def internal_reference
[@product.id, @product.name].join("-")
end
end
The view would then look like this;
<div id="product">
<h1><%= @product.name %></h1>
<h2><%= @internal_reference %></h2>
</div>
- All instance variables and local variables (
local_assigns
) are passed to the view model. - All attributes and public methods are exposed to the view
- You can override local variables in the view model with public methods
- You can add new local variables in the view model with public methods