Test the below examples in app/blogs/index.html.erb
.
#1 PORO helper components
<tbody>
<%= safe_join @blogs.map(&method(:blogs_table_row)) %>
</tbody>
<tbody>
<% @blogs.each do |blog| %>
<%= blogs_table_row(blog) %>
<% end %>
</tbody>
<tbody>
<% @blogs.each_with_index do |blog, index| %>
<%=
blogs_table_row blog, -> do
content_tag(:strong, "Ahoy #{'!' * (index+1)}")
end
%>
<% end %>
</tbody>
#2 Rails partial components
<tbody>
<% @blogs.each do |blog| %>
<%= component(:row, blog: blog) %>
<% end %>
</tbody>
<tbody>
<% @blogs.each_with_index do |blog, index| %>
<%= component(:row, blog: blog) do %>
<strong>Ahoy<%= '!' * (index+1) %></strong>
<% end %>
<% end %>
</tbody>
Test the following snippets in rails console
:
tags = Components::Tags.new(helper)
tags.br
#=> "<br />"
tags.br style: 'margin-top: 10px'
#=> "<br style=\"margin-top: 10px\" />"
tags.ul
#=> "<ul></ul>"
tags.ul [1,2,3].map &tags.method(:li)
#=> "<ul><li>1</li><li>2</li><li>3</li></ul>"