Display ActiveRecords separately

Hello everyone, I want display ActiveRecords separately.

Now it displayed one blob with multiple records in it. I want to display every record (picture) as a gallery, as a individual picture not as a blob (as a post).

I’m not sure if I used the correct vocabulary (Blob, ActiveRecords), but I hope it’s clear what I’m getting at.

Does anyone know how I can do that?

Greeting Tron


So, if I understood, your model is:

class Picture < ApplicationRecord
  has_many_attached :photos

And instead of showing the Picture that has many attached photos, you want to show the photos?

Simplest way:

class PhotoController < ApplicationController
  def show
    @blob = ActiveStorage::Blob.find(params[:id])

That said, if actually want to treat the images themselves as posts, with their own title, etc, then it’s better if you change your modeling:

class Gallery < ApplicationRecord
  has_many :photos

class Photo < ApplicationRecord
  has_one_attached :file
  validates :title, presence: true
  validates :description, presence: true

As a rule of thumb, you only use has_many_attached when you are dealing with things like email attachments, or stuff that is only downloaded. For image galleries it’s better to create a model specific for each Photo and use has_one_attached

Hello Breno, thank you for your help.

But I would like the user to be able to upload multiple images at once. Is this a problem?

Now I can see all the pictures but the link leads directly to the file. But I would like to have a show page for each picture to view more details.

# app/controllers/photos_controller.rb
  def index
    @photos = ActiveStorage::Blob.all
# app/views/photos/index.html.erb
<% @photos.each do |photo| %>
  <%= link_to image_tag(photo, style:"height:200px"), photo %>
<% end %>

I know it’s not the way you meant it. But so far this has given the best result.

Actually, I just wanted to create a simple picture gallery. But I still don’t really understand the whole ActiveStorage, ActiveRecord concept.

The Active Storage is maybe the wrong way or question. And I’ll leave it at that for now.

I will open a post that is more general.

Thanks to all