Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 22, 2022 11:03 pm GMT

How to use Devise with turbo in Rails 7

In Rails 7, hotwire's turbo intercepts form actions and submits them over the wire.

Currently, Devise gem does not support this action, hence needs a little tweaking to function well.

NB: This tutorial assumes you have installed and configured Devise gem already.

First let's create a controller, I will name mine app/controllers/turbo_devise_user_controller.rb

class TurboDeviseUserController < 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_streamend

The above controller will make Devise responds to turbo and rendering templates. But of course, this won't happen without a modifying our devise initializer, which is located at 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  endend Devise.setup do |config|  ...  # Configure the parent class to the custom controller.  config.parent_controller = 'TurboDeviseUserController'  # Warden configuration  config.warden do |manager|    manager.failure_app = TurboFailureApp  end...end

That's it! Now start you rails app and keep coding!!.


Original Link: https://dev.to/efocoder/how-to-use-devise-with-turbo-in-rails-7-9n9

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To