载波文件删除(Carrierwave file delete)

编程入门 行业动态 更新时间:2024-10-25 23:29:50
载波文件删除(Carrierwave file delete)

我再次需要你的帮助。 现在我需要了解如何使用Carrierwave上传的文件进行删除(在我的情况下 - 图像)。

models / attachment.rb:

class Attachment < ActiveRecord::Base belongs_to :attachable, :polymorphic => true attr_accessible :file, :file mount_uploader :file, FileUploader end

models / post.rb:

class Post < ActiveRecord::Base attr_accessible :content, :title, :attachments_attributes, :_destroy has_many :attachments, :as => :attachable accepts_nested_attributes_for :attachments end

* views / posts / _form.html.erb:*

<%= nested_form_for @post, :html=>{:multipart => true } do |f| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> <ul> <% @post.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div id="field"> <%= f.label :Nosaukums %>:<br /><br /> <%= f.text_field :title %><br /><br /> </div> <div id="field"> <%= f.label :Raksts %>:<br /><br /> <%= f.text_area :content %><br /><br /> </div> <%= f.fields_for :attachments do |attachment| %> <% if attachment.object.new_record? %> <%= attachment.file_field :file %> <% else %> <%= image_tag(attachment.object.file.url) %> <%= f.check_box :_destroy %> <% end %> <% end %> <%= f.submit "Publicēt", :id => "button-link" %> <% end %>

当我试图删除以前上传的文件时,我有这个错误:

unknown attribute: _destroy

也许有问题,因为我有多个文件上传不仅一个。

Again I need your help. Now I need to understand how I can delete with Carrierwave uploaded files (in my case - images).

models/attachment.rb :

class Attachment < ActiveRecord::Base belongs_to :attachable, :polymorphic => true attr_accessible :file, :file mount_uploader :file, FileUploader end

models/post.rb :

class Post < ActiveRecord::Base attr_accessible :content, :title, :attachments_attributes, :_destroy has_many :attachments, :as => :attachable accepts_nested_attributes_for :attachments end

*views/posts/_form.html.erb :*

<%= nested_form_for @post, :html=>{:multipart => true } do |f| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> <ul> <% @post.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div id="field"> <%= f.label :Nosaukums %>:<br /><br /> <%= f.text_field :title %><br /><br /> </div> <div id="field"> <%= f.label :Raksts %>:<br /><br /> <%= f.text_area :content %><br /><br /> </div> <%= f.fields_for :attachments do |attachment| %> <% if attachment.object.new_record? %> <%= attachment.file_field :file %> <% else %> <%= image_tag(attachment.object.file.url) %> <%= f.check_box :_destroy %> <% end %> <% end %> <%= f.submit "Publicēt", :id => "button-link" %> <% end %>

When I am trying to delete previous uploaded file I have this error:

unknown attribute: _destroy

Maybe there is problem because I have multiple file uploads not only one.

最满意答案

你在错误的模型上调用方法。 您的文件装载在附件上。

错误是告诉你什么是错的。

undefined method 'remove_file' for #<Post:0x471a320

错误的关键点在于需要在Attachment模型上调用Post模型时调用的方法。

也许尝试将复选框的输入范围设定为正确的模型。

<%= attachment.check_box :remove_file %>

You're calling the method on the wrong model. Your file mount is on the Attachment.

The error is telling you what is wrong.

undefined method 'remove_file' for #<Post:0x471a320

The key point of the error is the method is being called on the Post model when it needs to be called on the Attachment model.

Maybe try scoping the input for the checkbox to the correct model.

<%= attachment.check_box :remove_file %>

更多推荐

本文发布于:2023-08-03 14:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391338.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:载波   文件   Carrierwave   file   delete

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!