Rails & Devise: How to render login page without a layout?

后端 未结 3 1199
再見小時候
再見小時候 2021-01-30 01:11

I know this is probably a simple question, but I\'m still trying to figure Devise out...

I want to render :layout => false on my login page; how can I do

相关标签:
3条回答
  • 2021-01-30 01:29

    You can subclass the controller and configure the router to use that:

    class SessionsController < Devise::SessionsController
      layout false
    end
    

    And in config/routes.rb:

    devise_for :users, :controllers => { :sessions => "sessions" }
    

    You need to move the session views to this controller too.

    OR make a method in app/controllers/application_controller.rb:

    class ApplicationController < ActionController::Base
    
      layout :layout
    
      private
    
      def layout
        # only turn it off for login pages:
        is_a?(Devise::SessionsController) ? false : "application"
        # or turn layout off for every devise controller:
        devise_controller? && "application"
      end
    
    end
    
    0 讨论(0)
  • 2021-01-30 01:34

    You can also create a sessions.html.erb file in app/views/layouts/devise. That layout will then be used for just the sign in screen.

    0 讨论(0)
  • 2021-01-30 01:42

    By using the devise_controller? helper you can determine when a Devise controller is active and respond accordingly. To have Devise use a separate layout to the rest of your application, you could do something like this:

    class ApplicationController < ActionController::Base
      layout :layout_by_resource
    
      protected
    
      def layout_by_resource
        if devise_controller?
          "devise"
        else
          "application"
        end
      end
    end
    

    create a devise.html.erb file in your views/layouts

    So if its a device controller will render the devise layout else the application layout

    from: https://github.com/plataformatec/devise/wiki/How-To:-Create-custom-layouts

    0 讨论(0)
提交回复
热议问题