私は、モジュールを有効にして、Deviseを使用して認証をセットアップしたRails 3アプリを持っています。Rails:(Devise)新規ユーザー向けに2つの異なる方法がありますか?
私たちの外部登録フォームを使用してサインインして、Devise registerable
モジュール全体を使用したいと考えています。これは現在行われています。
しかし、admin
ユーザーは、Devitsのregisterable
モジュールをバイパスして、新しいユーザーを直接作成することもできます。 registerable
無効で
、私の標準がUserControllerは、他の鉄道の足場のように、私は
admin
ユーザーのためにそれを望むように動作します。しかし、今や新しいユーザーは自分で登録することはできません。registerable
有効になって、私の標準がUserControllerは、(代わりに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"=>"[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
ユーザーが手動でとゲストユーザが自分で登録することができ、新規ユーザーを作成することができるように、これらのメソッドの両方が一緒に仕事を得ることができますか?次のように
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
Duh!それは私が探していた秘密のソースでした。非常に多くの問題を解決する...私は壁に私の頭をバッシュ行く間、私を許してください。ありがとう。 – neezer
コントローラに名前空間を与える方法は? –
Railsで名前空間を使用する方法についてもう少し詳しく説明します。app> controllersにフォルダを作成する必要があります。この例では、新しいフォルダadminをコントローラフォルダに置き、新しいユーザコントローラをそこに配置します。このファイルは、applcom/admin/usersからアクセスできます。http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing –