为什么 rails 回形针在更新时删除附件而不单击复选框



我在 ruby on rails 站点的资源上使用回形针删除方法并尝试删除附件,我遇到的问题是即使我没有选中复选框删除图像,它仍然会为我删除它是错误的。这是我所拥有的:

想法.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 />rn<br />rn<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"}

请注意,即使我使用检查器查看复选框的值等于 1,"img_delete"=>"0"也为零,如下所示:

<input id="idea_img_delete" label="Delete Image" name="idea[img_delete]" type="checkbox" value="1">

现在无论如何,您都在清除#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因为这实际上是导致图像被删除的原因。

相关内容

  • 没有找到相关文章

最新更新