我想删除我插入多张图像的相册控制器中的单个图像我怎样才能删除?

问题描述

我已经使用设计用户设置了身份验证,并且我有我的相册控制器,我在其中插入了多个图像。到目前为止,我刚刚在我的相册控制器中创建了一个删除图像的方法

我的相册控制器如下所示:

class AlbumsController < ApplicationController
    before_action :authenticate_user!
    def index
        @albums = current_user.albums.all
    end
    def show 
        
        @album= current_user.albums.find(params[:id])
    end 
    def new
        @album = current_user.albums.new
    end
    def create 
        @album = current_user.albums.new(album_params)
        if @album.save
            redirect_to albums_path 
        else
            render :new
        end
    end
    def edit
        @album =current_user.albums.find(params[:id])
    end
    def update
        @album = current_user.albums.find(params[:id])
        if @album.update(album_params)
            redirect_to albums_path
        else
            render :edit
        end
    end
    def destroy
        @album = current_user.albums.find(params[:id])
        @album.destroy 
        redirect_to albums_path
    end
    def delete_image_attachment
        @image = current_user.albums.ActiveStorage::Blob.find(params[:id])
        @image.purge
        redirect_to albums_path
    end
      
    
    private
        def album_params
            params.require(:album).permit(:title,:desciption,images: [])
        end
end

你可以看到我创建了一个名为 delete_image_attachment..

现在我不知道在我的路由文件中输入什么:

Rails.application.routes.draw do

  devise_for :users 

  root to: "albums#index"
  resources :albums do 
    resources :comments
  end
  
end

给出路​​径后,我可以在我的显示页面中给出什么路径?

<%= link_to 'Remove',method: :delete,data: {confirm: "Are You Sure ?"} %> 

我的 Rails 路线是:

Prefix Verb   URI Pattern                                                                                       Controller#Action
                        new_user_session GET    /users/sign_in(.:format)                                                                          devise/sessions#new
                            user_session POST   /users/sign_in(.:format)                                                                          devise/sessions#create
                    destroy_user_session DELETE /users/sign_out(.:format)                                                                         devise/sessions#destroy
                       new_user_password GET    /users/password/new(.:format)                                                                     devise/passwords#new
                      edit_user_password GET    /users/password/edit(.:format)                                                                    devise/passwords#edit
                           user_password PATCH  /users/password(.:format)                                                                         devise/passwords#update
                                         PUT    /users/password(.:format)                                                                         devise/passwords#update
                                         POST   /users/password(.:format)                                                                         devise/passwords#create
                cancel_user_registration GET    /users/cancel(.:format)                                                                           devise/registrations#cancel
                   new_user_registration GET    /users/sign_up(.:format)                                                                          devise/registrations#new
                  edit_user_registration GET    /users/edit(.:format)                                                                             devise/registrations#edit
                       user_registration PATCH  /users(.:format)                                                                                  devise/registrations#update
                                         PUT    /users(.:format)                                                                                  devise/registrations#update
                                         DELETE /users(.:format)                                                                                  devise/registrations#destroy
                                         POST   /users(.:format)                                                                                  devise/registrations#create
                                    root GET    /                                                                                                 albums#index
                          album_comments GET    /albums/:album_id/comments(.:format)                                                              comments#index
                                         POST   /albums/:album_id/comments(.:format)                                                              comments#create
                       new_album_comment GET    /albums/:album_id/comments/new(.:format)                                                          comments#new
                      edit_album_comment GET    /albums/:album_id/comments/:id/edit(.:format)                                                     comments#edit
                           album_comment GET    /albums/:album_id/comments/:id(.:format)                                                          comments#show
                                         PATCH  /albums/:album_id/comments/:id(.:format)                                                          comments#update
                                         PUT    /albums/:album_id/comments/:id(.:format)                                                          comments#update
                                         DELETE /albums/:album_id/comments/:id(.:format)                                                          comments#destroy
                                  albums GET    /albums(.:format)                                                                                 albums#index
                                         POST   /albums(.:format)                                                                                 albums#create
                               new_album GET    /albums/new(.:format)                                                                             albums#new
                              edit_album GET    /albums/:id/edit(.:format)                                                                        albums#edit
                                   album GET    /albums/:id(.:format)                                                                             albums#show
                                         PATCH  /albums/:id(.:format)                                                                             albums#update
                                         PUT    /albums/:id(.:format)                                                                             albums#update
                                         DELETE /albums/:id(.:format)                                                                             albums#destroy
           rails_postmark_inbound_emails POST   /rails/action_mailBox/postmark/inbound_emails(.:format)                                           action_mailBox/ingresses/postmark/inbound_emails#create
              rails_relay_inbound_emails POST   /rails/action_mailBox/relay/inbound_emails(.:format)                                              action_mailBox/ingresses/relay/inbound_emails#create
           rails_sendgrid_inbound_emails POST   /rails/action_mailBox/sendgrid/inbound_emails(.:format)                                           action_mailBox/ingresses/sendgrid/inbound_emails#create
     rails_mandrill_inbound_health_check GET    /rails/action_mailBox/mandrill/inbound_emails(.:format)                                           action_mailBox/ingresses/mandrill/inbound_emails#health_check
           rails_mandrill_inbound_emails POST   /rails/action_mailBox/mandrill/inbound_emails(.:format)                                           action_mailBox/ingresses/mandrill/inbound_emails#create
            rails_mailgun_inbound_emails POST   /rails/action_mailBox/mailgun/inbound_emails/mime(.:format)                                       action_mailBox/ingresses/mailgun/inbound_emails#create
          rails_conductor_inbound_emails GET    /rails/conductor/action_mailBox/inbound_emails(.:format)                                          rails/conductor/action_mailBox/inbound_emails#index
                                         POST   /rails/conductor/action_mailBox/inbound_emails(.:format)                                          rails/conductor/action_mailBox/inbound_emails#create
       new_rails_conductor_inbound_email GET    /rails/conductor/action_mailBox/inbound_emails/new(.:format)                                      rails/conductor/action_mailBox/inbound_emails#new
      edit_rails_conductor_inbound_email GET    /rails/conductor/action_mailBox/inbound_emails/:id/edit(.:format)                                 rails/conductor/action_mailBox/inbound_emails#edit
           rails_conductor_inbound_email GET    /rails/conductor/action_mailBox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailBox/inbound_emails#show
                                         PATCH  /rails/conductor/action_mailBox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailBox/inbound_emails#update
                                         PUT    /rails/conductor/action_mailBox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailBox/inbound_emails#update
                                         DELETE /rails/conductor/action_mailBox/inbound_emails/:id(.:format)                                      rails/conductor/action_mailBox/inbound_emails#destroy
new_rails_conductor_inbound_email_source GET    /rails/conductor/action_mailBox/inbound_emails/sources/new(.:format)                              rails/conductor/action_mailBox/inbound_emails/sources#new
   rails_conductor_inbound_email_sources POST   /rails/conductor/action_mailBox/inbound_emails/sources(.:format)                                  rails/conductor/action_mailBox/inbound_emails/sources#create
   rails_conductor_inbound_email_reroute POST   /rails/conductor/action_mailBox/:inbound_email_id/reroute(.:format)                               rails/conductor/action_mailBox/reroutes#create
                      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

我创建的删除图像的方法有误吗?

解决方法

如果您希望能够单独处理 CRUD 图像,我建议您设置一个中间模型而不是使用 has_many_attached :images,因为实际路由到单个图像会非常尴尬。

这还为您提供了一个合乎逻辑的位置,可以用标题等内容来注释图像。 has_many_attached 有它的位置,但如果您附加的实际上是域对象,则不是。

resources :albums do
  resources :images,only: [:destroy]
end
class Album < ApplicationRecord
  has_many :images
end
module Image  < ApplicationRecord
  belongs_to :album
  has_one_attached :file
  # use callbacks to purge
end
class ImagesController < ApplicationController
  before_action :authenticate_user
  before_action :set_image
  before_action :authorize_image

  # DELETE /albums/1/images/2
  def destroy
    @image.destroy
    redirect_to @album,notice: 'Image removed'
  end

  private
  def set_image
    @album = Album.find(params[:album_id])
    @image = @album.images.find(params[:id])
  end

  # you should really consider using pundit or cancancan for authorization
  def authorize_image
    unless @album.user == current_user
      redirect_to root_path,error: "You're not authorized"
    end
  end
end 
<% @album.images.each do |image| %>
  <div class="album-image">
    <%= image_tag image.file %>
    <%= button_to 'Delete image',[@album,image],method: :delete %>
  </div>
<% end %>
,

正确吗?

@image = current_user.albums.ActiveStorage::Blob.find(params[:id])

我认为我们需要做

@image = current_user.albums.find(params[:id])

然后

@image.purge

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...