Search code examples
ruby-on-railsroutesrails-adminrails-activestorage

Delete function not working for Active Storage image in rails app


I’m trying to create a delete function for images that has been uploaded via active storage in my rails app.

I’m using devise for sign up, wicked wizard for multi step and cancancan for authorization. I'm also using rails admin.

But I get this error, and I can’t figure out how to make it work. I can see it has something to do with the routes, but i don't understand what the problem is and how to fix it. Any help would be much appreciated.

uninitialized constant RegistrationsController

my form

<%= form_for @user, url: wizard_path, method: :put do |f| %>

<% if @user.clinic_images.attached? %>

<% @user.clinic_images.each do |image| %>

<%= image_tag(image.variant(resize: "100x100")) %>
<%= link_to ‘Delete this image’, delete_image_attachment_registration_url(image.signed_id),

method: :delete,

data: { confirm: ‘Are you sure?’ } %>

<% end %>

<% end %>

<% end %>


Routes.rb

Rails.application.routes.draw do
  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  devise_for :users, controllers: {:registrations => "users/registrations"}
  resources :registration_steps
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'pages#index' 
  get 'about', to: 'pages#about'
  get 'team', to: 'pages#team'
  get 'faqs', to: 'pages#faqs'
  get 'faqspractitioners', to: 'pages#faqspractitioners'
  get 'faqsusers', to: 'pages#faqsusers'
  get 'login', to: 'pages#login'
  get 'signup', to: 'pages#signup'
  get 'search', to: 'pages#search'



  devise_scope :users do 
    resources :registrations do

      member do

        delete :delete_image_attachment

        end

    end
  end

end


registrations_controller.rb

  # frozen_string_literal: true

class Users::RegistrationsController < Devise::RegistrationsController
  def delete_image_attachment

    @image = ActiveStorage::Blob.find_signed(params[:id])

    @image.purge_later

    redirect_to root_path

    end


  # before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  # def create
  #   super
  # end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

 protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_up_params
  #   devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
  # end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
  # end

  # The path used after sign up.



def after_sign_up_path_for(resource)
  registration_steps_path
 end


 def after_update_path_for(resource)

  registration_steps_path

end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end




end

user.rb

class User < ApplicationRecord

  def current_step?(step_key) current_step == step_key end

    enum gender: { Mand: 0, Kvinde: 1 }

    def self.genders_for_select
    genders.keys.map{ |x| [x.humanize, x] }
    end

    has_one_attached :clinic_logo

    has_one_attached :practitioner_image
    has_many_attached :clinic_images


    # Note that implicit association has a plural form in this case
    scope :with_eager_loaded_images, -> { eager_load(images_attachments: :blob) }

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

         after_create :send_admin_mail
         def send_admin_mail
           UserMailer.send_welcome_email(self).deliver_later
         end


end

Solution

  • You have set your route resources wrong.

    devise_scope :user do 
      scope module: :users do
        resources :registrations do
          member do
            delete :delete_image_attachment
          end
        end
      end
    end
    

    You can read more about it here: https://rubyinrails.com/2019/04/16/rails-routes-difference-between-resource-and-resources/