簡體   English   中英

如何在Rails 4中創建銷毀操作以刪除文件?

[英]How to create a destroy action in rails 4 to remove files?

我正在使用refile gem進行多次上傳。 圖像顯示很好。 我想創建一個銷毀動作,以使用link_to在圖像下顯示。 我應該如何編寫我的銷毀動作, link to的路徑應該是什么? 謝謝!

show.html.erb

<% @post.images.each do |i| %>
 <%= attachment_image_tag(i, :file, :fill, 350, 350, format: "jpg") %>
 <%= link_to 'Delete', **?????**, method: :detele %>
<% end %>

我可以使用控制台從我的第一條帖子中刪除第二張圖片,例如p=Post.first.images.find 2然后是p.destroy

我的帖子和圖片模型是

class Image < ActiveRecord::Base
  belongs_to :post
  attachment :file
end

class Post < ActiveRecord::Base
  has_many :images, dependent: :destroy
  accepts_attachments_for :images, attachment: :file, append: true
end

config / routes.rb

Rails.application.routes.draw do resources :posts do resources :images end root 'posts#index' end

<% @post.images.each do |i| %>
  <%= attachment_image_tag(i, :file, :fill, 350, 350, format: "jpg") %>
  <%= link_to 'Delete', post_image_path(@post.id, i.id), method: :detele %>
<% end %>

只需粘貼您的圖像路徑。 如果您的路由配置正確,那么就足夠了。

更新

您的路線應該是這樣的

resources :posts do
  resources :images
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM