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

时间:2010-12-20 16:55:27

标签: ruby-on-rails devise

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

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

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

  • 禁用registerable后,我的标准UsersController就像我希望的那样为admin用户工作,就像任何其他的Rail脚手架一样。但是,现在新用户无法自行注册。

  • 启用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"=>"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 < 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

2 个答案:

答案 0 :(得分:12)

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

config/routes.rb

devise :users # Allow users to register here

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

答案 1 :(得分:0)

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

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

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

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

  ## other actions 
end

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