我有回形针宝石的问题。我正在使用Rails 4.我想为用户创建可能性,以编辑图像附件。现在,当我编辑扬声器时,我只能上传新图像,不能编辑现有图像。
= 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的解决方案无法帮助我。
答案 0 :(得分:1)
在您的控制器中
def remove_picture
speaker = Speaker.where(id: params[:id]).first
speaker.image.destroy
redirect_to request.referer
end
在您的视图中创建以下链接 -
- 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
尝试以上代码。
答案 1 :(得分:0)
您是否希望用户能够像在Photoshop或类似(可能更简单)的图像编辑应用中那样编辑图像?如果是这样,那么你最好的选择是使用javascript插件,例如这些jQuery插件之一:
http://www.sitepoint.com/image-manipulation/ http://www.jqueryrain.com/2014/11/picedit-jquery-front-end-image-editor-plugin/
这些只是通过谷歌搜索" jquery图像编辑器插件"。
另请参阅此相关问题:Javascript image editing plugin
您需要根据您希望为用户提供的编辑选项类型,选择最适合您的选项。