0
votes

I am using a paperclip delete method on my resource in ruby on rails site and trying to delete an attachment, the problem I have is even when I do not select the checkbox to delete the image it still deletes it for me which is wrong. Here is what I have:

idea.rb model

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

then in my controller update I have this:

    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

Then in my form where the delete checkbox is I have this:

<div class="field">
   <h3>Delete image(save idea to delete featured image)</h3>
   <%= f.check_box :img_delete, :label => 'Delete Image' %>
</div>

Can anyone tell me why it deletes the image even though I do not check the checkbox. This is what gets output in the console for anyones interest:

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"}

Notice the "img_delete"=>"0" is zero even though when I view with inspector the value of the checkbox is equal to 1 like so:

<input id="idea_img_delete" label="Delete Image" name="idea[img_delete]" type="checkbox" value="1">
1
@idea.featured_image.clear this line in #update seems like it will always clear the image.engineersmnky
I know but this is what clears it on check of fieldM dunbavan
What do you mean "this is what clears it on the check of field" all I see is you directly calling clear without conditions.engineersmnky
The conditions are in the model so that's why I am confused.M dunbavan

1 Answers

0
votes

Right now you are clearing the image on #update no matter what. Try this:

In 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

In controller(#update) remove @idea.featured_image.clear because this is what is actually causing the image to be deleted.