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
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.
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.