我已经使用devise user设置了身份验证,因为我有我的相册控制器,我在其中插入了多个图像。到目前为止,我已经在我的相册控制器中创建了一个删除图像的方法。
我的相册控制器看起来像这样:
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我创建的删除图片的方法是不是错了?
发布于 2021-04-16 15:48:13
如果你想要单独的CRUD镜像,我建议你建立一个中间模型,而不是使用has_many_attached :images,因为实际上路由到独立的镜像将是相当尴尬的。
这也给了你一个逻辑的地方来记录图片,比如标题。has_many_attached有它的位置,但如果你附加的是一个域对象,就不是这样了。
resources :albums do
resources :images, only: [:destroy]
endclass Album < ApplicationRecord
has_many :images
endmodule Image < ApplicationRecord
belongs_to :album
has_one_attached :file
# use callbacks to purge
endclass 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 %>发布于 2021-04-16 15:13:35
这是正确的吗?
@image = current_user.albums.ActiveStorage::Blob.find(params[:id])我想我们需要做的
@image = current_user.albums.find(params[:id])然后
@image.purgehttps://stackoverflow.com/questions/67119423
复制相似问题