I want to display a random assortment of 6 tools from my database on my home page. I created a Pages controller with a home action.
This is my page controller:
class PagesController < ApplicationController def home @tools = Tool.all end end
Then, in my home.html.erb view, I use the .sample method to grab random tools from my database as such (repeat this 6 times using the variables tool1, tool2, tool3, etc. for each):
<% tool1 = @tools.sample %> <%= image_tag tool1.tool_image.url(:medium) %> <%= tool1.name %> <%= tool1.description %>
I am wondering if there is a better way to do this. It seems that I have a logic, in my opinion, and should there be a way to move this logic somewhere else? My model, controller, etc. How could you clear this code so that it performs well? Or maybe this is a good rails code, and I just don't know it since I start.
source share