RAILS PLUGIN: Ajax Scaffold File Upload plugin
Here’s a simple but elegant solution to handling file uploads with the marvelous Ajax Scaffold plugin. It seamlessly extends (not replaces) the ajax-scaffold plugin so posts can be made via an iframe, allowing the possibility of file uploads. Because it extends, and not replaces, you can continue to update your copy of ajax-scaffold-plugin without having to back and redo anything!
install via script/plugin install svn://rubyforge.org/var/svn/timcharper/ajaxscaffoldp_upload
================================================
USAGE:
- Install ajax-scaffold, the plugin version. You can find it at http://www.ajaxscaffold.com/
- Install ajax_scaffold upload by executing “script/plugin install svn://rubyforge.org/var/svn/timcharper/ajaxscaffoldp_upload”
- Follow the directions for ajax_scaffold as normal
- In your controller, replace the line:
ajax_scaffold :model, :rows_per_page => 15
TO
ajax_scaffold_upload :model, :rows_per_page => 15
================================================
The plugin is transparent and you should notice no noticeable differences (other than it’s using an iframe for posting)
Enjoy!
Notice:
This plugin ships with responds-to-parent. Responds-to-parent was created by Sean Treadway
January 29th, 2007 at 2:36 pm
This is great! Thanks!
February 9th, 2007 at 1:28 pm
Very nice
I do have one issue though - this piece of code seems to be breaking my shared partials:
if partial_path.include?('/')
if (erb_template_exists?("#{path}/_#{partial_name}"))
path, partial_name = partial_pieces(partial_name)
return "#{path}/#{partial_name}"
end
end
I do have an slightly unusual situation - most of my controllers are in an ‘admin’ directory (e.g. ‘admin/products’), and the shared templates are simply under ‘admin’.
The result is this:
render :partial => ‘admin/header’
results in
No rhtml, rxml, rjs or delegate template found for admin/products/_header in ./script/../config/../app/views
Can I safely remove the section of code I pasted above?
Thanks,
–YY
February 9th, 2007 at 1:38 pm
This is my (quick) fix, which seems to work for me so far:
if partial_path.include?(’/')
if (erb_template_exists?(”#{path}/_#{partial_name}”))
#path, partial_name = partial_pieces(partial_name)
return “#{path}/#{partial_name}”
else
path, partial_name = partial_pieces(partial_name)
if (erb_template_exists?(”#{path}/_#{partial_name}”))
return “#{path}/#{partial_name}”
end
end
end
Hopefully I got the formatting right this time
February 9th, 2007 at 3:25 pm
This may be a silly question but how do I mark a database field to be a file upload?
February 16th, 2007 at 8:00 am
Excellent work - this is exactly what I’m looking for - have you thought about getting this working with edge?
March 4th, 2007 at 4:08 am
It doesn’t seem to be rendering partials correctly…could easily be a problem on my end, but…
ActionView::ActionViewError in Content_pages#view_default
Showing app/views/layouts/application.rhtml where line #46 raised:
No rhtml, rxml, rjs or delegate template found for content_pages/_suckerfish in script/../config/../app/views
Extracted source (around line #46):
43: ‘page/menubar’, :action =>
44: ’suckerfish’) %–>
45: ‘menu_items/suckerfish’,
46: :locals => {:items => session[:menu].get_menu(0)} %>
47:
48:
49:
RAILS_ROOT: script/../config/..
Application Trace :
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_view/base.rb:399:in `find_template_extension_for’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_view/base.rb:335:in `pick_template_extension’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_view/base.rb:249:in `render_file’
vendor/plugins/ajaxscaffoldp/lib/ajax_scaffold_plugin.rb:467:in `render’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_view/partials.rb:59:in `render_partial’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:26:in `benchmark’
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure’
/usr/lib/ruby/1.8/benchmark.rb:307:in `realtime’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:26:in `benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_view/partials.rb:58:in `render_partial’
vendor/plugins/ajaxscaffoldp/lib/ajax_scaffold_plugin.rb:481:in `render’
#{RAILS_ROOT}/app/views/layouts/application.rhtml:46:in `_run_rhtml_47app47views47layouts47application46rhtml’
It should look for _suckerfish.rhtml in the app/views/menu_items folder, but it’s looking in the app/views/content_pages folder…I think it has to do with the parse_partial_path method on line 488 of vendor/plugins/ajaxscaffoldp/lib/ajax_scaffold_plugin.rb (called from the render method living right before it in the code), but this doesn’t happen before install / after remove of ajaxscaffoldp_upload.
Could this have to do with being in an iframe? I’m pretty new to all this, so…
Thanks for anyone’s thoughts.
March 12th, 2007 at 4:48 pm
Nice work, a real time saver! Much thanks!