Components on Rails views
If you already have plenty of Rails views (ERB, Haml or Slim) and want to start creating small UI components in pure Ruby, you are able to use components on these existing views.
app/matestack/components/products/teaser.rb
1
class Components::Products::Teaser < Matestack::Ui::Component
2
3
requires :product
4
5
def response
6
a path: product_path(context.product), class: 'product-teaser' do
7
div do
8
h2 context.product.name
9
paragraph conext.product.description
10
b context.product.price
11
end
12
end
13
end
14
15
end
Copied!
The class is then called on your Rails view, in this case an ERB view:
1
<%= @products.each do |product| %>
2
<%= Components::Products::Teaser.(product: product) %>
3
<% end %>
Copied!
This approach is suitable for existing apps and a good idea to migrate to Matestack step by step. If you start with a blank Rails app, we recommend to go full Matestack right away!
Last modified 6mo ago
Copy link