different layout for sign_in action in devise

后端 未结 8 2183
情深已故
情深已故 2020-12-02 05:12

I\'m trying to use a different/custom layout named \"devise\" for the sign_in action. I found this page in the devise wiki, and the second example even says you can do it pe

8条回答
  •  天涯浪人
    2020-12-02 05:33

    This is how I did it. I wanted a different layout if the user had to sign in, but a different layout if the user had to edit his/her profile.

    I am using Rails 4.1.1

    In the application controller, add this :

    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
      before_action :configure_permitted_parameters, if: :devise_controller?
    
      layout :layout_by_resource
    
      # Define the permitted parameters for Devise.
      protected
    
      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:firstname, :lastname, :email, :password, :password_confirmation)}
        devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:avatar, :firstname, :lastname, :email, :password, :password_confirmation, :current_password) }
      end
    
      def layout_by_resource
        if devise_controller? and user_signed_in?
          'dashboard'
        else
          'application'
        end
      end
    end
    

提交回复
热议问题