Skip to content

Instantly share code, notes, and snippets.

@boardfish
Last active February 3, 2022 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boardfish/5eafa75c28fdc2e248b8a0c6e9133964 to your computer and use it in GitHub Desktop.
Save boardfish/5eafa75c28fdc2e248b8a0c6e9133964 to your computer and use it in GitHub Desktop.
Example of a basic table component
<p style="color: green"><%= notice %></p>
<h1>Products</h1>
<div id="products">
<%= render TableComponent.new do |c| %>
<% @products.each do |product| %>
<% c.row(product, RowComponent) %>
<% end %>
<% end %>
</div>
<%= link_to "New product", new_product_path %>
<tr>
<% @object.attributes.each do |key, value| %>
<td id="<%= "#{helpers.dom_id(@object)}_#{key}" %>"><%= value %></td>
<% end %>
</tr>
# frozen_string_literal: true
class RowComponent < ApplicationComponent
def initialize(object)
@object = object
end
end
<table>
<thead>
<% column_names.each do |column_name| %>
<th><%= column_name %></th>
<% end %>
</thead>
<tbody>
<% rows.each do |row| %>
<%= row %>
<% end %>
</tbody>
</table>
# frozen_string_literal: true
class TableComponent < ApplicationComponent
renders_many :rows, ->(object, component_class, **params) { component_class.new(object, **params) }
def column_names
Product.new.attributes.keys
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment