Reputation: 57
I am working in Ruby on Rails 6.0 and up with Shrine and trying to get the first photo of my images to be the cover image of my Album page. I am working from the Rails-Shrine-Example here:
https://github.com/erikdahlstrand/shrine-rails-example
My goal is to show the first photo from the uploaded photos as the cover photo and not the cover_photo in the album table. Is this possible?
My code for the album cover is:
<% @albums.each do |album| %>
<tr>
<td scope="row"><%= image_tag album.cover_photo_url(:small), size: "200x200", class: "img-thumbnail" %></td>
<td scope="row"><%= album.name %></td>
<td scope="row"><%= link_to "Edit", album, class: "btn btn-default" %> <%= link_to "Show", album, class: "btn btn-default" %></td>
</tr>
<% end %>
Then inside my album I was able to pull the first photo from the album with
<% @album.photo.each do |photo| %>
<div class="col-lg-3 col-md-4 col-6">
<%= image_tag photo.image.derivation_url(:thumbnail, 300, 300).to_s, :class => "img-fluid img-thumbnail" %>
</div>
<% end %>
So that I try to explain it all, my db has an album table with the title and cover photo and there is another table for photos.
How can I tell it to select for each album show first image from that album's photo table?
*UPDATE I was able to put this script up and it works to some degree except the each loop shows the first photo in my photo table the number of times equal to the number of photos uploaded. I know its my loop.
<% @albums.each do |album| %>
<tr>
<td scope="row">
<% album.photos.each do |photo| %>
<%= image_tag album.photos.first.image.derivation_url(:thumbnail, 300, 300), :class => "img-fluid img-thumbnail" %>
<% end %>
</td>
<td scope="row"><%= image_tag album.cover_photo_url(:small), size: "200x200", class: "img-thumbnail" %></td>
<td scope="row"><%= album.name %></td>
<td scope="row"><%= link_to "Edit", album, class: "btn btn-default" %> <%= link_to "Show", album, class: "btn btn-default" %></td>
</tr>
<% end %>
Photo Model
class Photo < ActiveRecord::Base
include ImageUploader::Attachment(:image) # ImageUploader will attach and manage `image`
end
Album Model
class Album < ApplicationRecord
has_many :photos, dependent: :destroy
accepts_nested_attributes_for :photos, allow_destroy: true
include ImageUploader::Attachment(:cover_photo) # ImageUploader will attach and manage `cover_photo`
validates_presence_of :name, :cover_photo # Normal model validations - optional
end
As Lurker had shared that I was also pulling from albums that didn't have photos attached. I am not sure what to do to fix the string. I know some image_tags use the if present? or something but I cannot add it to this without errors.
Upvotes: 1
Views: 179
Reputation: 57
Ok so I think I figured it out. With the string of conditions on the photo I cannot do a simple if present? argument. I needed to create a if else statement. So here is what I found works for me.
<% if album.photos.present? %>
<%= image_tag album.photos.first.image.derivation_url(:thumbnail, 300, 300).to_s %>
<% else %>
<%= image_tag 'image.jpg' %>
<% end %>
Upvotes: 1