删除功能不适用于 rails 应用中的 Active Storage 图像
Delete function not working for Active Storage image in rails app
我正在尝试为通过我的 rails 应用中的活动存储上传的图像创建删除功能。
我使用 devise 进行注册,使用 wicked wizard 进行多步操作,使用 cancancan 进行授权。我也在使用 rails 管理员。
但是我收到了这个错误,我不知道如何让它工作。我可以看到它与路线有关,但我不明白问题是什么以及如何解决。任何帮助将非常感激。
我的表格
<%= 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
您的路由资源设置有误。
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/
我正在尝试为通过我的 rails 应用中的活动存储上传的图像创建删除功能。
我使用 devise 进行注册,使用 wicked wizard 进行多步操作,使用 cancancan 进行授权。我也在使用 rails 管理员。
但是我收到了这个错误,我不知道如何让它工作。我可以看到它与路线有关,但我不明白问题是什么以及如何解决。任何帮助将非常感激。
我的表格
<%= 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
您的路由资源设置有误。
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/