我在我的资源上使用回形针删除方法在ruby on rails网站上并尝试删除附件,我遇到的问题是即使我没有选中复选框来删除图像它仍然删除它对我来说这是错误。这就是我所拥有的:
idea.rb模型
class Idea < ActiveRecord::Base
include PgSearch
pg_search_scope :search, :against => [:title, :slug, :idea_type, :body],
using: { tsearch:{ dictionary: "english" } }
def self.text_search(query)
if query.present?
where("title @@ :q or body @@ :q or idea_type @@ :q", q: query)
else
scoped
end
end
paginates_per 6
has_attached_file :featured_image, :styles => { :medium => "300x300>", :thumb => "100x100>", :crop_cover => "390x240>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :featured_image, :content_type => /\Aimage\/.*\Z/
validates :idea_type, :title, :slug, :body, presence: true
before_save :destroy_image?
extend FriendlyId
friendly_id :title, use: [:slugged, :finders]
def should_generate_new_friendly_id?
title_changed?
end
def img_delete
@featured_image_delete ||= "0"
end
def img_delete=(value)
@featured_image_delete = value
end
private
def destroy_image?
self.featured_image.clear if @featured_image_delete == "1" and !featured_image.dirty?
end
end
然后在我的控制器更新中我有这个:
def update
@idea.featured_image.clear
respond_to do |format|
if @idea.update(idea_params)
format.html { redirect_to @idea, notice: 'Idea was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @idea.errors, status: :unprocessable_entity }
end
end
end
def idea_params
params.require(:idea).permit(:title, :featured_image, :img_delete, :video, :author_id, :body, :idea_type, :provided_by_logo, author_attributes: [:name,:biography])
end
然后在我删除复选框的表单中,我有:
<div class="field">
<h3>Delete image(save idea to delete featured image)</h3>
<%= f.check_box :img_delete, :label => 'Delete Image' %>
</div>
任何人都可以告诉我为什么它会删除图像,即使我没有勾选复选框。这是在控制台中为任何人感兴趣的输出:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y4dEALXAMAEexn+ztgI5mkZ/aIUsysRRCAlYUVgjhD4=", "idea"=>{"user_id"=>"50", "title"=>"A test", "img_delete"=>"0", "video"=>"<iframe width=\"960\" height=\"515\" src=\"https://www.youtube.com/embed/JlP4ZppEcJ8\" frameborder=\"0\" allowfullscreen></iframe>", "body"=>"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere consectetur est at lobortis. Sed posuere consectetur est at lobortis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.<br />\r\n<br />\r\n<img alt=\"\" src=\"/ckeditor_assets/pictures/4/content_croc-cover.jpg\" style=\"height:506px; width:600px\" />", "idea_type"=>"make"}, "commit"=>"Update Idea", "id"=>"a-test"}
请注意, "img_delete"=>"0"
为零,即使我使用检查器查看复选框的值等于1,如下所示:
<input id="idea_img_delete" label="Delete Image" name="idea[img_delete]" type="checkbox" value="1">
答案 0 :(得分:0)
现在,无论如何,您都要清除#update
上的图像。试试这个:
在models/idea.rb
class Idea < ActiveRecord::Base
attr_accessor :featured_image_delete
before_save(on: :update) do
self.featured_image.clear if self.featured_image_delete == "1" and !self.featured_image.dirty?
end
end
在控制器(#update
)中删除@idea.featured_image.clear
,因为这实际上是导致图像被删除的原因。