Redirecting AJAX forms with Rails

A very short example of how to redirect to a controller provided url after an AJAX form submit inside a Ruby on Rails application.

If you need to submit a form via jQuery to a Rails application, you may find that your regular redirects won't work. This is because technically the request is being redirected but the client browser won't follow. You can quickly solve this issue by instead redirecting in your clientside script when the form endpoint responds. Your form submit may look like this:

function sendFormData() {
  var form = $('#yourFormId').closest('form');
  form = form.serializeArray();
  form = form.concat([
    { name: 'extraInfo': value: JSON.stringify(extraInfo) }
  $.post('/form-endpoint', form, function(data) {
    if (data.error) {

Your controller could redirect like this:

def form_endpoint
  flash[:notice] = "Success! Form was posted."
  render js: "window.location = '#{some_path}'"

When the clientside script receives the form response it will either pause and alert the user of the error, or it will redirect to the path you specified in your controller.