2013-07-23 40 views
0

我想阻止註冊非管理員,以便只有超級用戶/管理員才能添加新用戶。我怎樣才能做到這一點?Rails 4設計,如何登錄新用戶?

我試着按照這裏提到的方法:Devise before filter that prevents access to 「new_user_registration_path」 unless user is signed-in但沒有結果。

我已經安裝了devise,cancan和rolify。另外,我也不希望任何人訪問/users/sign_up頁面並登錄。只有管理員必須有能力註冊新用戶。

由於設計安裝沒有用戶控制器。如果需要,請指導我做一個。

routes.rb

FifthApp::Application.routes.draw do 
    devise_for :users 
    resources :qanotes 
end 

user.rb

class User < ActiveRecord::Base 

    #resourcify :resources 
    rolify 

    has_many :qanotes 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

end 

我一直被重定向到根,即本地主機:3000,當我嘗試去註冊頁面(只有在登錄後)。

回答

0

刪除:從用戶模型的默認設計模塊中刪除可重複使用的關鍵字。比你應該添加自己的表單來創建新用戶,因此你可以添加新用戶。 希望它會有所幫助。由於

0

你可以customize registration controller(例如registrations_controller.rb),並且你應該添加before_filter認證和before_filter只有管理員才能registrations_controller.rb樣子:

class RegistrationsController < Devise::RegistrationsController 
before_filter :authenticate_user! 
before_filter :is_administratior?, only: [:new, :create] 

def new 
    super 
end 

def create 
    super 
end 

private 

    def is_administratior? 
    if user_signed_in? # if user signed 
    if current_user.administrator? # if adminstrator return true 
    true 
    else 
    redirect_to some_path 
    end 
    else 
    redirect_to login_path 
    end 
end 

my answer here about Devise/cancan redirect admin

相關問題