编辑并销毁图像回形针 rails

Edit and destroy image paperclip rails

回形针有问题gem。我正在使用 Rails 4. 我想为用户提供编辑图像附件的可能性。目前编辑Speaker时只能上传新图,无法编辑已有图片

= 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

但是,我无法使用 :delete_image 创建复选框输入,所以现在,每次刷新站点都会破坏图像(因为@speaker.save)。

可以给我一些建议,如何解决? Rails 3 的解决方案对我没有帮助。

您是说您希望用户能够像在 Photoshop 或类似(可能更简单)的图像编辑应用程序中那样编辑图像吗?如果是这样,那么您最好的选择是使用 javascript 插件,例如这些 jQuery 插件之一:

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

这些是通过谷歌搜索得到的"jquery image editor plugin"。

另请参阅此相关问题:Javascript image editing plugin

您需要根据您希望为用户提供的编辑选项类型选择最适合您的选项。

在你的控制器中

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

在您看来创建以下 link -

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

在您的 routes.rb 中定义相同的路线

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

试试上面的代码。