Skip to content

Instantly share code, notes, and snippets.

@eclectic-coding
Last active January 30, 2021 20:35
Show Gist options
  • Save eclectic-coding/a18d18970569a6e8a30c61c19710deba to your computer and use it in GitHub Desktop.
Save eclectic-coding/a18d18970569a6e8a30c61c19710deba to your computer and use it in GitHub Desktop.
Devise fix for

Create new controllers

Add the following to the top of config/initializers/devise.rb:

class TurboFailureApp < Devise::FailureApp
  def respond
    if request_format == :turbo_stream
      redirect
    else
      super
    end
  end

  def skip_format?
    %w(html turbo_stream */*).include? request_format.to_s
  end
end

class TurboController < ApplicationController
  class Responder < ActionController::Responder
    def to_turbo_stream
      controller.render(options.merge(formats: :html))
    rescue ActionView::MissingTemplate => error
      if get?
        raise error
      elsif has_errors? && default_action
        render rendering_options.merge(formats: :html, status: :unprocessable_entity)
      else
        redirect_to navigation_location
      end
    end
  end

  self.responder = Responder
  respond_to :html, :turbo_stream
end

Enable a new parent controller around line 52: config.parent_controller = 'TurboController'

Enable navigational_formats(around line 297): config.navigational_formats = ['*/*', :html, :turbo_stream]

In the Warden configuration (around line 315) enable the following:

config.warden do |manager|
  manager.failure_app = TurboFailureApp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment