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

Ruby on-RailsDevise

Ruby on-Rails Problem Overview


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 this with Devise?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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.

Solution 3 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneezerView Question on Stackoverflow
Solution 1 - Ruby on-RailsiainView Answer on Stackoverflow
Solution 2 - Ruby on-RailsPaul RaupachView Answer on Stackoverflow
Solution 3 - Ruby on-RailsmsrootView Answer on Stackoverflow