p8 / multipart_form_for
- Source
- Commits
- Network (1)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
Petrik (author)
Sun Jun 14 01:37:02 -0700 2009
| name | age | message | |
|---|---|---|---|
| |
.autotest | ||
| |
MIT-LICENSE | ||
| |
README.markdown | ||
| |
Rakefile | ||
| |
init.rb | ||
| |
install.rb | ||
| |
lib/ | ||
| |
tasks/ | ||
| |
test/ | ||
| |
uninstall.rb |
README.markdown
MultipartFormFor
Create multipart Ajax forms. There is a security restriction with javascript to prevent access to the filesystem. To get around the AJAX/file upload problem we make use of the iframe remoting pattern. It's based on the following blog post: http://khamsouk.souvanlasy.com/2007/5/1/ajax-file-uploads-in-rails-using-attachment_fu-and-responds_to_parent
Example
In your View:
<div id="product_<%= @product.id %>" >
<% multipart_remote_form_for(@product) do |f| %>
<%= f.error_messages %>
<%= f.file_field :image, :onchange => 'this.form.submit()' %>
<% end %>
</div>
which results in:
<form enctype="multipart/form-data" class="edit_post" action="/posts/#{post.id}" method="post"
id="edit_post_#{post.id}" target="upload_frame_for_#{post.id}">
<div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div>
<input id="product_image" name="product[image]" onchange="this.form.submit()" size="30" type="file" />
</form>
<iframe id="upload_frame_for_#{post.id}"
name="upload_frame_for_#{post.id}"
style="width:1px;height:1px;border:1px"
src="about:blank"></iframe>
And in your Controller:
def update
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
responds_to_parent do
render(:update) do |page|
page.replace_html("product_#{@product.id}",
{:partial => 'form', :locals => { :product => @product }})
end
end
end
end
Install
It requires the responds_to_parent plugin
script/plugin install git://github.com/markcatley/responds_to_parent.git
script/plugin install git://github.com/p8/multipart_form_for.git
Copyright (c) 2009 Petrik de Heus, released under the MIT license

