I have a store with Products. I created a _product.html.erb partial that has a summary of the product, and I use it for both the index and show pages.
The issue is, I want the product name to be an <h1> heading in the product show page, but an <h2> on the index page. I want to do this because I have many product on the index page, and I’m not sure if it makes sense to have a large number of <h1>s on the same page. For the show page, I only have that one product, so definitely want to use the <h1>.
The way I did this was with an if/else clause in the partial as below, but it’s a bit messy. Is there a better way to do this?
product/_product.html.erb
<div class="product_partial">
<% if params[:action] == "show" %>
<h1><%= product.name %></h1>
<% else %>
<h2><%= product.name %></h2>
<% end %>
<h3><%= product.description %></h3>
</div>
product/show.html.erb
<div class="product_show">
<%= @product %>
<%= @product.other_info %>
</div>
product/index.html.erb
<div class="product_index">
<%= @products %>
</div>
Maybe something like this
product/_product.html.erb
product/show.html.erb
product/index.html.erb