使用Devise管理用户管理

我是第一次尝试Devise。 我想做的其中一件事是为管理员用户提供创建,查找和编辑用户的界面。 这是我可能出错的地方。

我创建了一个PeopleController类,它inheritance自ApplicationController,它列出了人员并提供了创建和更新用户的方法和视图。 除了一个例外,一切正常。 当管理员用户更新他们自己的记录时,会话被清除,他们必须在保存后再次登录。

在这个应用程序中,我没有使用可注册模块。 只有管​​理员用户才能创建新用户。 设计用户管理工具的正确方法是什么。 创建我自己的控制器似乎是错误的选择。

在此先感谢您的帮助。

非常感谢你的帮助。 这基本上就是我正在做的事情。 我发现了一个线索,帮助我解决用户会话在这个wiki中编辑自己的记录时被清除的问题:

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

这是我需要的线路:

sign_in resource_name, resource, :bypass => true 

这个方法位于Devise :: Controllers :: Helpers中,所以我在我的控制器中做了这个。

 class PeopleController < ApplicationController include Devise::Controllers::Helpers 

然后在我的update方法中,只有当current_user.id等于正在编辑的id时才调用它:

 def update @person = User.find(params[:id]) if @person.update_attributes(params[:user]) sign_in @person, :bypass => true if current_user.id == @person.id redirect_to person_path(@person), :notice => "Successfully updated user." else render :action => 'edit' end end 

现在,如果当前用户编辑自己的记录,则会话在保存后将恢复。

再次感谢您的回复。

这就是我在其中一个应用中管理用户的方式。 我只生成了一个User

 rails g devise User 

我通过此迁移添加了一个role列:

 class AddRoleToUser < ActiveRecord::Migration def change add_column :users, :role, :string, :default => "client" end end 

和我的User模型:

 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, :lockable and :timeoutable devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me def admin? self.role == "admin" end end 

然后创建新用户所有你需要做的就是在控制器中提供一个自定义方法(甚至可能是子类Devise::RegistrationsController ),如下所示:

 # some_controller.rb def custom_create_user if current_user.admin? User.create(:email => params[:email], password => params[:password]) redirect_to(some_path, :notice => 'sucessfully updated user.') else redirect_to(some_other_path, :notice => 'You are not authorized to do this.') end end