2010-12-20 63 views
11

我有一个使用Devise并启用registerable模块的身份验证设置的Rails 3应用程序。Rails :(设计)面向新用户的两种不同方法?

我想要使用我们的外部注册表格注册的新用户使用现在正在发生的完整Devise registerable模块。

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

  • 随着registerable残疾,我的标准UsersController工程,我希望它为用户admin,就像任何其他的铁支架。但是,现在新用户无法自行注册。

  • 随着registerable启用,我的标准UsersController绝不会为新的用户操作(调用Devise::RegistrationsController替代),和我的CRUD的行为似乎并没有在所有的工作(我被人抛弃背到我的根页没有新用户创建并没有Flash消息)。这里的日志从请求:

    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"=>"[email protected]", "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 < ApplicationController 
    load_and_authorize_resource 

    def index 
    @users = User.where("id NOT IN (?)", current_user.id) # don't display the current user in the users list; go to account management to edit current user details 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     flash[:notice] = "#{ @user.email } created." 
     redirect_to users_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 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 

回答

12

你应该创建一个单独的控制器来管理您的用户。我总是建立管理员用户,并给他们一个特殊的命名空间中工作让我说明:

config/routes.rb

devise :users # Allow users to register here 

namespace :admin do 
    resources :users # Have the admin manage them here. 
end 
+3

呃!那是我一直在寻找的秘密武器。解决了很多问题......对不起,而我撞我的头靠在墙上。谢谢。 – neezer 2010-12-21 16:39:57

+0

如何在控制器中提供命名空间? – 2012-03-05 07:37:54

+0

下面是关于如何在Rails中使用名称空间的更多细节。您将需要在应用程序>控制器中创建一个文件夹 - 在本例中,您将在控制器文件夹中放置一个新的文件夹admin,然后将您的新用户控制器放在那里。这些文件应该可以通过applcom/admin/users访问 - http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing – 2012-05-12 00:25:23

0

你需要在你的routes.rb添加此配置

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

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

class Users::RegistrationsController < Devise::RegistrationsController 
    def new 
    ... 
    end 

    ## other actions 
end 

然后你可以写你自己的观点 如果你的重写设计的默认控制器,也许你会失去一些功能,例如验证。你需要自己执行它们

相关问题