Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

"respond_with" via responders ignores JavaScript redirections #12

Open
glennfu opened this issue Aug 1, 2016 · 0 comments
Open

"respond_with" via responders ignores JavaScript redirections #12

glennfu opened this issue Aug 1, 2016 · 0 comments

Comments

@glennfu
Copy link

glennfu commented Aug 1, 2016

When rendering a form with remote: true, one can replace the form with an error-rendered state via AJAX. However, using the very popular "responders" conventions, like Devise does, you find that the success response looks like:

respond_with resource, location: after_sign_up_path_for(resource)

while the error response looks like

respond_with resource

The problem here being that the turbolinks-rails handling of the redirection in the success case never happens. In fact, no attempt at redirection is made. It simply tries to render a JS template for the action.

I'm not sure if a fix really belongs inside turbolinks-rails, but this issue is of course specific to turbolinks-rails. So if anyone else is pondering about the same problem, here's a simple fix:

# config/initializers/responders.rb
module ActionController
  class Responder

    # Default implementation of `to_js` is just `default_render` but if there's no errors and
    # we pass a location then I expect to be redirected via Turbolinks JS
    def to_js
      if !has_errors? && options[:location].present?
        redirect_to options[:location]
      else
        default_render
      end
    end

  end
end
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant