Controller Before editing
gears_controller.rb
def index
@user = current_user
@gear = Gear.where(user_id: @user.id)
end
After editing
gears_controller.rb
def index
@user = current_user
@gear1 = Gear.where(user_id: @user.id, category: "Residential system")
@gear2 = Gear.where(user_id: @user.id, category: "Cooking system")
@gear3 = Gear.where(user_id: @user.id, category: "Fire system")
@gear4 = Gear.where(user_id: @user.id, category: "Other")
end
View
html:index.html.erb
<div class="category bg-success">Residential system</div>
<% @gear1.each do |gear| %>
<div class="gear-index-item mb-20">
<% if gear.image.attached? %>
<%= image_tag gear.image, class: "index-img" %>
<% else %>
<img class="index-img" src="<%= "/images/default_gear.jpg " %>" alt="Index image cap">
<% end %>
<%= link_to(gear.name, "/gears/#{gear.id}") %>
</div>
<% end %>
<div class="category bg-warning">Cooking system</div>
<% @gear2.each do |gear| %>
<div class="gear-index-item mb-20">
<% if gear.image.attached? %>
<%= image_tag gear.image, class: "index-img" %>
<% else %>
<img class="index-img" src="<%= "/images/default_gear.jpg " %>" alt="Index image cap">
<% end %>
<%= link_to(gear.name, "/gears/#{gear.id}") %>
</div>
<% end %>
.
.
.
Iterate with each statement.
Recommended Posts