Функция удаления не работает для образа Active Storage в приложении rails - PullRequest
1 голос
/ 03 февраля 2020

Я пытаюсь создать функцию удаления для изображений, которые были загружены через активное хранилище, в моем приложении rails.

Я использую devise для регистрации, wicked wizard для многошаговой и cancancan для авторизации , Я также использую rails admin.

Но я получаю эту ошибку, и я не могу понять, как заставить ее работать. Я вижу, что это как-то связано с маршрутами, но я не понимаю, в чем проблема и как ее исправить. Любая помощь приветствуется.

uninitialized constant RegistrationsController

моя форма

<%= 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

1 Ответ

1 голос
/ 03 февраля 2020

Вы неправильно указали ресурсы маршрута.

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

Подробнее об этом можно прочитать здесь: https://rubyinrails.com/2019/04/16/rails-routes-difference-between-resource-and-resources/

...