Devise within namespace

后端 未结 6 736
慢半拍i
慢半拍i 2021-01-30 03:22

I\'m trying to split my rails project in a front-end for regular users and a back-end for admins. Therefore i have created a namespace \'admin\' so that i can easily control adm

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 03:43

    Simply "moving" Devise to the admin namespace is wrong. Devise uses controllers like Devise::SessionsController and that cannot be "moved".

    I usually create my own controllers and inherit them from Devise:

    class Admin::SessionsController < ::Devise::SessionsController
      layout "admin"
      # the rest is inherited, so it should work
    end
    

    And configure this in config/routes.rb:

    devise_for :admins, :controllers => { :sessions => "admin/sessions" }
    

    Or you could change the layout only, by making the layout a bit more complex:

    class ApplicationController < ActionController::Base
    
      layout :layout
    
      private
    
      def layout
        if devise_controller? && devise_mapping.name == :admin
          "admin"
        else
          "application"
        end
      end
    
    end
    

提交回复
热议问题