Skip to content

Instantly share code, notes, and snippets.

@dsazup
Last active December 31, 2020 04:45
Show Gist options
  • Save dsazup/82eab47282512258d232302451b0aacd to your computer and use it in GitHub Desktop.
Save dsazup/82eab47282512258d232302451b0aacd to your computer and use it in GitHub Desktop.
make devise work with turbo streams
# need to update devise config with these settings
config.parent_controller = 'Users::DeviseController'
class Users::DeviseController < ApplicationController
self.responder = TurboDeviseResponder
respond_to :turbo_stream, :html
end
"dependencies": {
"@hotwired/turbo": "https://github.com/hotwired/turbo/archive/dev-builds/latest.tar.gz",
}
class TurboDeviseResponder < ActionController::Responder
def initialize(controller, resources, options = nil)
super
options[:formats] = :html
end
def to_turbo_stream
return to_html if get?
unless has_errors?
return redirect_to navigation_location
end
display_errors
end
protected
def display_errors
controller.render(rendering_options.merge(status: :unprocessable_entity))
end
def rendering_options
super.merge! formats: :html
end
def has_errors?
return true if controller.flash.alert.present?
super
end
end
@pjo336
Copy link

pjo336 commented Dec 31, 2020

I don't even have the failure app and I seem to be pretty close. What does that portion provide for us?

@dsazup
Copy link
Author

dsazup commented Dec 31, 2020

I don't even have the failure app and I seem to be pretty close. What does that portion provide for us?

I run into some issues where it adds .turbo_stream to the url 🤷‍♂️ but since this PR hotwired/turbo#39 got merged, all we need to do now is to return 422 if there are any errors. I updated the example, seems to work good for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment