jquery - AJAX image upload with Rails, Backbone, Remotipart -


i've been using remotipart upload images via ajax while now, we've decided go backbone route, , since forms rendered via js templates, i'm no longer able use :remote => true syntax generate iframe transport code. don't want have figure out how manually. thoughts on how can remotipart work when using js templates?

figured out solution. little confused on how js remotipart works. after little research, here's found:

remotipart works binding ajax:aborted:file event, triggered when jquery ujs (which handles traditional ajax form submits) detects file within form. since script (along dependent iframe transport plugin) included on page, need manually add

data-remote="true" 

to form. jquery has .on() event bound forms such attributes, if include that, remotipart triggered.

another issue, though, if form submitted, still fail upload file due lack of csrf token. solution manually add hidden input tag token.

here's final markup worked me (i'm using .eco templates little jquery grab value of csrf meta tags on page):

<form accept-charset="utf-8" id="image-upload-form" action="/projects/<%= @project.get('id') %>" method="post" enctype="multipart/form-data" data-remote="true">     <input name="project[cover_photo]" type="file" />     <input type="hidden" name="<%= $('meta[name="csrf-param"]').attr('content') %>" value="<%= $('meta[name="csrf-token"]').attr('content') %>" />     <input name="_method" type="hidden" value="put" />     <button id="add-photo">save</button> </form> 

hope spare else same trouble had!


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -