Submitting form data with ajax and no ajax

Hi, I’m dealing with a multistep form that is initially an ajax form with remote: true. Midway through the form, I want to go ahead and save the object before the final submit button (for example, on step 3 of 5). The way I have it set up currently is that in the create.js.erb, I modify attributes of the form in step3 so that the form submits to the update action (and no longer to the create action). On the final step, the form is supposed to be sent without ajax and the user is redirected to another page. However, what I’ve discovered is that Rails binds the ajax to the form and it is oddly difficult to make a form non-ajax after initially setting it to remote: true. In the create.js.erb, I have tried the following without success (the form is still submitted with ajax on the final step):

$('.form#new_listing').unbind();
$('.form#new_listing').removeAttr("data-remote");

In the create.js.erb, I also have the following which changes the form from handling a new object to handling an existing object:

$('form').attr("class", "edit_listing");
$('form').attr("id", "edit_listing_<%= @listing.id %>");
$('form').attr("action", "<%= listing_path(@listing) %>");
$('form').attr("method", "put");

Has anyone else dealt with a similar situation? I feel like this may be a good situation to move away from the Rails conventions, but wanted to get more opinions. I am completely open to entirely changing the way I’m handling this. The form is actually on a single page and the different slides are shown and hidden with css and javascript. Thanks!

Hi @realDLee,

Is there a reason why you can’t submit the form with ajax both times? I believe that is the approach I would take, given what you are trying to accomplish.

thanks,
-Chad

@cpytel Only because I want to redirect after the final slide of the form… is manually changing the form to submit to the ‘update’ action like I’ve done the right way to handle this? Thanks!

Yes, change the form action, but you can keep both requests Ajax. In update.js.erb just have

window.location = '<%= path_helper %>'