I’m working on a photo queue (think of a carousel that displays a single image at a time) that allows the user to upvote/downvote/skip each image. To reduce lag time, I want the next 2-3 photos that will be shown next to be preloaded in the background and ready to go. I haven’t quite figured out the best way to to accomplish this. My current setup is to get the first image in the queue in the Index action of the controller.
class PhotosController < ApplicationController def index @photo = Photo.unvoted.first end end
Then when the user votes on the image, the update.js.erb is rendered and is redirected to the Index action where the next image that has not been voted on is shown.
The voting form looks like this:
<%- 3.times do |n| %> <%= form_for @photo, remote: true do |f| %> <%= f.hidden_field :photo_rating, value: n %> <%= f.submit n, class: "btn" %> <% end %> <% end %>
My main two questions are:
(1) How can I minimize the number of queries? This probably involves modifying the controller action.
(2) What’s a good way to handle preloading images the Rails way?
Even if you have advice or suggestions related to other issues, I’d love to hear them. Thanks!