Pavel
Pavel

Reputation: 1974

Rails: different layouts

In my app I want to use different layouts when user is logged in and not

application_controller:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  layout :determine_layout

  private

    def determine_layout
      user_signed_in? ? 'application' : 'landing'
    end

end

But this code not working: layouts not changing. Could Devise cause this? Or some mistakes I made?

Thanks!

Upvotes: 0

Views: 159

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

Was going to write as comment, but it will be easier to process here:

According to the Devise github wiki, you could determine whether it's based on devise_controller or not:

class ApplicationController < ActionController::Base
  layout :layout_by_resource

  protected

  def layout_by_resource
    if devise_controller?
      "layout_name_for_devise"
    else
      "application"
    end
  end
end

This would allow you to point the user to sessions#landing if they weren't logged in, which would mean creating a new action in a custom Devise sessions controller

This would DRY-up the process of determining if they are logged-in or not (if they are not logged in, they can see static views from the session or registrations controllers of Devise

Upvotes: 1

Related Questions