So after searching for a while I found an answer and it isn't pretty. You have to pretty much replace the file upload field with a new one using javascript.
Here is a basic example using jQuery:
In the view:
<%= f.file_field :attachment %>where id and name should be the same as your file field. Right now, I'm trying to figure out a way of pulling this info out cleanly from the form builder but not having much progress.
<%= link_to_function 'clear', clear_js(id, name) %>
In a helper:
# creates an equivalent file field and replaces the current oneFYI, I read that you can't just alter the value of the field with something like $('input').value = ' ', and for a good reason - then someone could theoretically write a script to set the value to any file on your computer.
def clear_js(id, name)
%Q[
var newButton = document.createElement("input");
$(newButton).attr("type","file");
$(newButton).attr("id", "#{id}");
$(newButton).attr("name", "#{name}");
$(newButton).attr("size", "30");
$("##{id}").replaceWith(newButton);
]
end
No comments:
Post a Comment