Rails:(Devise)新用户有两种不同的方法? [英] Rails: (Devise) Two different methods for new users?
问题描述
我有一个Rails 3应用程序,其认证设置使用 Devise 与可注册
I have a Rails 3 app with authentication setup using Devise with the registerable
module enabled.
我想让新用户使用我们的外部注册表单注册使用完整的设计可以注册的
模块,现在正在发生。
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的可注册
模块。
However, I also want the admin
user to be able to create new users directly, bypassing (I think) Devise's registerable
module.
-
禁用
可注册
,我的标准的UsersController工作正如我想要的admin
用户就像任何其他铁脚手架一样。但是,现在新用户无法自行注册。
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.
启用可注册
UsersController从未被调用过新的用户操作(调用 Devise :: RegistrationsController
而不是),我的CRUD操作似乎没有工作(我被转回到我的root页面,没有新用户创建,没有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"=>"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
...但我可以通过外部表单注册新用户。
... 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:(Devise)新用户有两种不同的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!