我正在使用Devise作为身份验证模块的Rails应用程序,但我想自定义它,以便CanCan只允许管理员创建新用户。我很难理解如何为Devise定制控制器,以便完成这项工作。任何帮助,将不胜感激。
答案 0 :(得分:5)
您无需自定义任何内容:D
删除:可从您的Devise模型注册。
创建用户CRUD *(仅限脚手架用户)
使用CanCan获取用户的用户权限 控制器。
*检查Devise的wiki如何创建用户CRUD,你需要做一个路由技巧
答案 1 :(得分:0)
您可以创建一个“用户”控制器来管理用户,然后只需为其设置权限。因此,在您的新用户控制器中,您可以使用以下内容:
class UserController < ApplicationController
load_and_authorize_resource
def index
end
def new
end
def show
end
def create
if @user.save
flash[:notice] = "Successfully created User."
redirect_to root_path
else
render :action => 'new'
end
end
def edit
end
def update
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
if @user.update_attributes(params[:user])
flash[:notice] = "Successfully updated User."
redirect_to user_index_path
else
render :action => 'edit'
end
end
def destroy
if @user.destroy
flash[:notice] = "Successfully deleted User."
redirect_to user_index_path
end
end
end
假设您将管理员设置为:
can :manage, :all
你应该好好去。
在路线档案中,您需要设置路线:
resources :user, :controller => "user"
希望这有帮助!