Last active
December 31, 2020 04:45
-
-
Save dsazup/82eab47282512258d232302451b0aacd to your computer and use it in GitHub Desktop.
make devise work with turbo streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# need to update devise config with these settings | |
config.parent_controller = 'Users::DeviseController' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Users::DeviseController < ApplicationController | |
self.responder = TurboDeviseResponder | |
respond_to :turbo_stream, :html | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"dependencies": { | |
"@hotwired/turbo": "https://github.com/hotwired/turbo/archive/dev-builds/latest.tar.gz", | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
I don't even have the failure app and I seem to be pretty close. What does that portion provide for us?
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
The only problem on login
resource.valid?
fillsresource.errors
array, so each error message gets displayed. By default devise doesn't show errors on login, it just shows a flash message.