问题描述
我有一个带有身份验证设置的 Rails 3 应用程序,使用 Devise 和 registerable
模块已启用.
I have a Rails 3 app with authentication setup using Devise with the registerable
module enabled.
我想让新用户使用我们的外部注册表进行注册,以使用完整的 Devise registerable
模块,现在正在发生.
I want to have new users who sign up using our outside register form to use the full Devise registerable
module, which is happening now.
但是,我也希望 admin
用户能够直接创建新用户,绕过(我认为)Devise 的 registerable
模块.
However, I also want the admin
user to be able to create new users directly, bypassing (I think) Devise's registerable
module.
在禁用
registerable
后,我的标准 UsersController 可以像我希望的那样为admin
用户工作,就像任何其他 Rail 脚手架一样.但是,现在新用户无法自行注册.
With
registerable
disabled, my standard UsersController works as I want it to for theadmin
user, just like any other Rail scaffold. However, now new users can't register on their own.
启用 registerable
后,我的标准 UsersController 永远不会被调用来执行新的用户操作(而是调用 Devise::RegistrationsController
),并且我的 CRUD 操作不会t 似乎根本没有工作(我被转回到我的根页面上,没有创建新用户,也没有 flash 消息).这是请求的日志:
With registerable
enabled, my standard UsersController is never called for the new user action (calling Devise::RegistrationsController
instead), and my CRUD actions don't seem to work at all (I get dumped back onto my root page with no new user created and no flash message). Here's the log from the request:
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
...但我可以通过外部表单注册新用户.
... but I am able to register new users through the outside form.
如何让这两种方法一起工作,以便我的 admin
用户可以手动创建新用户 和 来宾用户可以自己注册?
How can I get both of these methods to work together, such that my admin
user can manually create new users and guest users can register on their own?
我为标准 CRUD 设置了我的用户控制器:
I have my Users controller setup for standard 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
推荐答案
您应该创建一个单独的控制器来管理您的用户.我总是创建管理员用户并给他们一个特殊的命名空间来工作.让我举例说明:
You should create a separate controller to manage your users. I always create administrator users and give them a special namespace to work in. Let me illustrate that:
config/routes.rb
devise :users # Allow users to register here
namespace :admin do
resources :users # Have the admin manage them here.
end
这篇关于Rails:(设计)针对新用户的两种不同方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!