I have multiple models that their indexes views are nearly share the exact same code, and layout.
.../chocolates/index.html.erb
<h1 class="col-sm-12 head">Index Bases</h1>
<hr>
<% @chocolates.each do |chocolate| %>
<%= link_to chocolate do %>
<%= set_img(chocolate) %>
<% end %>
<ul>
<li><%= chocolate.name %></li>
<li><%= chocolate.position %></li>
</ul>
<hr>
<div>
<%= link_to chocolate do %>
<span class="glyphicon glyphicon-eye-open"></span>
<% end %>
<%= link_to edit_chocolate_path(chocolate) do %>
<span class="glyphicon glyphicon-edit"></span>
<% end %>
<%= link_to basis, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</div>
<% end %>
<hr>
<%= link_to '+ Add New Chocolate', new_chocolate_path %>
.../sweets/index.html.erb
<h1 class="col-sm-12 head">Index Bases</h1>
<hr>
<% @sweets.each do |sweet| %>
<%= link_to sweet do %>
<%= set_img(sweet) %>
<% end %>
<ul>
<li><%= sweet.name %></li>
<li><%= sweet.position %></li>
</ul>
<hr>
<div>
<%= link_to sweet do %>
<span class="glyphicon glyphicon-eye-open"></span>
<% end %>
<%= link_to edit_sweet_path(sweet) do %>
<span class="glyphicon glyphicon-edit"></span>
<% end %>
<%= link_to sweet, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</div>
<% end %>
<hr>
<%= link_to '+ Add New Sweet', new_sweet_path %>
And there are more models that share the same layout, And I thought that I keep repeating my self so I created a shared partial with variables to render in each view that uses that layout using.
.../sweets/index.html.erb
<% render 'shared/indexGrid', dist: @sweets%>
.../views/shared/_indexGrid.html.erb
<% title = dist.class.name.underscore.tr('_', ' ').pluralize.split.map(&:capitalize).join(' ') %>
<% sing = dist.class.name.underscore %>
<h1>Index <% title %></h1>
<hr>
<% dist.each do |sing| %>
<%= link_to sing do %>
<%= set_img(sing) %>
<% end %>
<ul>
<li><%= sing.name %></li>
<li><%= sing.position %></li>
</ul>
<hr>
<div>
<%= link_to sing do %>
<span class="glyphicon glyphicon-eye-open"></span>
<% end %>
<%= link_to send("edit_#{sing.class.name.underscore}_path", sing) do %>
<span class="glyphicon glyphicon-edit"></span>
<% end %>
<%= link_to sing, class: 'square red', method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-remove"></span>
<% end %>
</div>
<% end %>
<hr>
<%= link_to "+ Add New #{title}", send("new_#{sing.class.name.underscore}_path"), { class: 'btn btn-success btn-block'} %>
But It didn't seems to work because -I think- dist.class.name deasnt return the value I was expecting but returns "active_record/relation".
I've tried a same approach before with edit views and It worked with dist: @sweet.
I've Thought of using layouts but It left me with big chunks of code that are very similar to the other index view.
I've shared my attempts with you, And the questions are...
- What's wrong with my code and how to fix it?
- Is this approach considered to be a good practice for DRY code? and If not What is the best way to share same view code with multiple models when those models are nearly shares the exact same code and layout?