Rails :(设计)新用户有两种不同的方法吗?

我有一个Rails 3应用程序,使用Devise并启用了可registerable模块进行身份validation设置。

我希望让使用我们的外部注册表单注册的新用户使用现在正在发生的完整的Devise可registerable模块。

但是,我还希望admin用户能够直接创建新用户,绕过(我认为)Devise的可registerable模块。

  • 通过可registerable禁用,我的标准UsersController可以像admin用户一样工作,就像任何其他Rail支架一样。 但是,现在新用户无法自行注册。

  • 启用registerable ,我的标准UsersController永远不会被调用新的用户操作(改为调用Devise::RegistrationsController ),我的CRUD操作似乎根本不起作用(我被转移到我的根页面而没有创建新用户)没有闪光信息)。 这是来自请求的日志:

     Started POST "/users" for 127.0.0.1 at 2010-12-20 11:49:31 -0500 Processing by Devise::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"18697r4syNNWHfMTkDCwcDYphjos+68rPFsaYKVjo8Y=", "user"=>{"email"=>"test@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"manager"}, "commit"=>"Create User"} SQL (0.9ms) ... User Load (0.6ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1 SQL (0.9ms) ... Redirected to http://test-app.local/ Completed 302 Found in 192ms 

…但我可以通过外部表单注册新用户。

如何让这两种方法协同工作,以便我的admin用户可以手动创建新用户, 访客用户可以自己注册?


我有标准CRUD的用户控制器设置:

 class UsersController  '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 users_path else render :action => 'edit' end end def delete end def destroy redirect_to users_path and return if params[:cancel] if @user.destroy flash[:notice] = "#{ @user.email } deleted." redirect_to users_path end end end 

我的路线设置如下:

 TestApp::Application.routes.draw do devise_for :users devise_scope :user do get "/login", :to => "devise/sessions#new", :as => :new_user_session get "/logout", :to => "devise/sessions#destroy", :as => :destroy_user_session end resources :users do get :delete, :on => :member end authenticate :user do root :to => "application#index" end root :to => "devise/session#new" end 

您应该创建一个单独的控制器来管理您的用户。 我总是创建管理员用户并为他们提供一个特殊的命名空间。让我说明一下:

config/routes.rb

 devise :users # Allow users to register here namespace :admin do resources :users # Have the admin manage them here. end 

您需要在routes.rb中添加此配置

 devise_for :users, :controllers => {:registrations => "users/registrations" } 

然后像这样实现你自己的注册控制器

 class Users::RegistrationsController < Devise::RegistrationsController def new ... end ## other actions end 

如果您的覆盖设备的默认控制器,您可以编写自己的视图,也许您将丢失一些function,例如validation。 你需要自己实现它们