Simple picture gallery

Hello everyone, I would like to create a picture gallery.

A user should be able to upload one or more pictures at once and delete one or more pictures.

Unfortunately I don’t know how to go about this task. Examples and tutorials don’t cover such thing. This should only be a simple picture gallery, as you can often find on the internet. Is my plan so unusual?

Many thanks. Tron

Seems quite doable. Try a tutorial like Hartl to get the basics of RoR because what you are trying to do is not trivial.

GitHub - cmckni3-boneyard/image-gallery: Image Gallery application built on Ruby on Rails does that help? There are a number of other hits.

  • Setup activestorage as normal.
  • Create a resource Example: Gallery
  • In the Gallery model you would use has_many_attached :images
  • On the form that you want to upload the images for the File Field you would add the multiple attribute true Example:<%= form.file_field :images, multiple: true %>

To delete the images I am not sure exactly how to delete multiple, but single you would call something like gallery.image.purge You may have to play around with this.

Here are some references:

Thank you all for the great posts. But unfortunately it doesn’t really help me.

That’s actually how it should work. But it doesn’t. Here’s my code. Maybe someone will notice something.

app/models/post.rb
# app/models/post.rb
class Post < ApplicationRecord
  has_many_attached :images
end
config/routes.rb
# config/routes.rb
Rails.application.routes.draw do
  resources :posts
  delete 'attachments/:id/purge', to: 'posts#purge', as: 'purge_attachment'
end
app/controllers/attachments_controller.rb
# app/controllers/attachments_controller.rb
class AttachmentsController < ApplicationController
  def purge
    attachment = ActiveStorage::Attachment.find(params[:id])
    attachment.purge
    redirect_back fallback_location: root_path, notice: "success"
  end
end
app/views/posts/show.html.erb
# app/views/posts/show.html.erb
  <% if @post.images.attached? %>
    <% @post.images.each do |attachment| %>
      <br>
      <% if attachment.image? %>
        <%= image_tag attachment, width: "200px" %>
      <% else %>
        <%= link_to attachment.filename, rails_blob_path(attachment, disposition: "attachment") %>
      <% end %>
      <br>
        <%= link_to attachment.filename, rails_blob_path(attachment) %>
      <br>
      <%= attachment %>
      <%= link_to "Delete", purge_attachment_path(attachment), method: :delete %>

    <% end %>
  <% end %>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
rails routes
# rails routes
                           Prefix Verb   URI Pattern                                                                                       Controller#Action
                            posts GET    /posts(.:format)                                                                                  posts#index
                                  POST   /posts(.:format)                                                                                  posts#create
                         new_post GET    /posts/new(.:format)                                                                              posts#new
                        edit_post GET    /posts/:id/edit(.:format)                                                                         posts#edit
                             post GET    /posts/:id(.:format)                                                                              posts#show
                                  PATCH  /posts/:id(.:format)                                                                              posts#update
                                  PUT    /posts/:id(.:format)                                                                              posts#update
                                  DELETE /posts/:id(.:format)                                                                              posts#destroy
                 purge_attachment DELETE /attachments/:id/purge(.:format)                                                                  posts#purge
 turbo_recede_historical_location GET    /recede_historical_location(.:format)                                                             turbo/native/navigation#recede
 turbo_resume_historical_location GET    /resume_historical_location(.:format)                                                             turbo/native/navigation#resume
turbo_refresh_historical_location GET    /refresh_historical_location(.:format)                                                            turbo/native/navigation#refresh
               rails_service_blob GET    /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format)                               active_storage/blobs/redirect#show
         rails_service_blob_proxy GET    /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format)                                  active_storage/blobs/proxy#show
                                  GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                                        active_storage/blobs/redirect#show
        rails_blob_representation GET    /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
  rails_blob_representation_proxy GET    /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format)    active_storage/representations/proxy#show
                                  GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)          active_storage/representations/redirect#show
               rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                                       active_storage/disk#show
        update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                               active_storage/disk#update
             rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                                    active_storage/direct_uploads#create
Screenshot from 'http://127.0.0.1:3000/posts/1'

versions
ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
rails -v
Rails 7.0.4

But I get the following error on this page: http://127.0.0.1:3000/attachments/2/purge

No route matches [GET] "/attachments/2/purge"

Does anyone know whats wrong with it?

Thank you for your attention.