Rails的文件上传(回形针)的编辑 [英] Rails file upload (paperclip) on edit

查看:189
本文介绍了Rails的文件上传(回形针)的编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己做了一个简单的Rails博客类的应用程序,我用回形针上传图片files.I拥有一切工作的罚款和花花公子。我甚至把它迷上了一个S3桶等漂亮吧?

I made myself a simple rails blogging-type app where I use Paperclip to upload image files.I have everything working fine and dandy. I even have it hooked up to an S3 bucket, etc. Spiffy right?

但我想不通编辑/更新帖子的时候该怎么办。由于是代表现在,我只有这个领域对我的表单模板:

But I can't figure what to do when EDITING/UPDATING a post. As is stands now, all I have is this field on my form template:

= f.file_field :image

所以,说在后/ 5 /编辑即使有一个previously附加的图像,该字段显示没有文件选择。

So, say on "post/5/edit" even if there's a previously attached image, the field displays "No file chosen".

更糟糕的是,有没有明显的方法来清除当前的图像,如果我改变了主意,不想附加的图像。

Even worse, there's no apparent way to clear out current image if I change my mind and don't want to attach an image.

我如何使这个多一点人性化,并确保当前图像 - 文字/网址是好的 - 显示为文本字段和/或用户的值可以改变当前图像无

How do I make this a little more user-friendly and make sure the current image -- text/url is fine -- shows up as the value in the text field and/or the user can change the current image to none.

推荐答案

有关Rails3这里就是我做的。对不起,我没有使用过Rails4呢。

For Rails3 here's what I do. Sorry, I haven't used Rails4 yet.

要显示给用户,如果他们已经上传的文件,这样在您的看法:

To display to the user if they have already uploaded a file, do this in your view:

<% if @blog.image.exists? %>
  <%= image_tag @blog.image.url(:thumb) %><br/>
<% end %>
<%= f.file_field :image %>

随后,以允许用户删除当前文件的上传添加到您的视图(如果块中):

Then, to allow for the user to remove the current upload add this to your view (inside that if block):

<%= f.check_box :delete_image %>Delete Image<br/>

和你处理这个复选框,在模型中:

And you handle that checkbox in your model:

  before_validation { image.clear if @delete_image }

  def delete_image
    @delete_image ||= false
  end

  def delete_image=(value)
    @delete_image  = !value.to_i.zero?
  end

这样,如果用户设置的复选框,将清除图像上的下一个保存。

That way if the user sets the checkbox it will clear the image on the next save.

这篇关于Rails的文件上传(回形针)的编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆