vendredi 17 avril 2015

prevent rails ajax form from reloading page

I know this is a common issue, but none of the results I've found fit my situation. I'm submitting a form with ajax, and I've been able to either submit with no page reload AND no dynamic partial loading, OR with a page reload rendering the partial. What I'm after is no page reload AND a partial being rendered on form submission.


HTML



<div id="form">
<%= form_tag :action => 'create', :remote => true do %>
<!-- SOME FORM FIELDS HERE -->
<%= image_submit_tag '', :id => 'submit-button', :class => 'button' %>
<% end %>
</div>


JS



$.ajax({
type: "POST",
action: "create",
dataType: 'script',
data: { SOME-DATA-HERE },
success: handleSuccessfulSubmission()
}).error(function (data) {
console.log('ajax failed')
});

return false;


RUBY



def create

respond_to do |format|

### DO SOME PROCESSING OF FORM DATA HERE

format.js
end

end


CREATE.JS.ERB



$("#container").html("<%= j (render partial: 'partial') %>");

Aucun commentaire:

Enregistrer un commentaire