我再次需要你的帮助。 现在我需要了解如何使用Carrierwave上传的文件进行删除(在我的情况下 - 图像)。
models / attachment.rb:
class Attachment < ActiveRecord::Base belongs_to :attachable, :polymorphic => true attr_accessible :file, :file mount_uploader :file, FileUploader endmodels / 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 endmodels/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: _destroyMaybe 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:0x471a320The 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 %>更多推荐
发布评论