Pigius
Pigius

Reputation: 41

Edit and destroy image paperclip rails

I have problem with paperclip gem. I'm using Rails 4. I would like to make possiblity for user, to edit image attachment. For now when I'm editing Speaker, I can only upload new image, no edit existing one.

= simple_form_for(@speaker) do |f|
  = f.error_notification
  .form-inputs
    = f.input :first_name
    = f.input :nickname
    = f.input :last_name
    = f.input :description
  - if @speaker.image.exists?
    = image_tag @speaker.image.url(:medium)
    = f.input :delete_image, as: :fake
    = @speaker.image = nil
    = @speaker.save
  = f.input :image
  .form-actions
    = f.button :submit

However, I can't create checkbox input with :delete_image, so for now, with every refresh site it destroys image (because of @speaker.save).

Could give me some advice, how to fix it ? Solutions from Rails 3 cant help me.

Upvotes: 1

Views: 1159

Answers (2)

LHH
LHH

Reputation: 3323

In your controller

def remove_picture      
   speaker = Speaker.where(id: params[:id]).first 
   speaker.image.destroy
   redirect_to request.referer
end

in your view create following link -

- if @speaker.image.exists?
   = link_to "Remove Image", remove_image_path(@speaker), method: :delete

in your routes.rb define routes for same

delete'/controller/remove_image/:id' => 'controller#remove_image', as: :remove_image

Try above code.

Upvotes: 1

Max Williams
Max Williams

Reputation: 32933

Do you mean you want the user to be able to edit the image like they would in Photoshop, or similar (perhaps more simple) image editing apps? If so then your best option is to use a javascript plugin, eg one of these jQuery plugins:

http://www.sitepoint.com/image-manipulation/ http://www.jqueryrain.com/2014/11/picedit-jquery-front-end-image-editor-plugin/

These were got just by googling "jquery image editor plugin".

See also this related question: Javascript image editing plugin

You will need to choose which one is the best fit for you based on the types of editing options you want to give your user.

Upvotes: 0

Related Questions