2010-12-20 4 views
11

私は、モジュールを有効にして、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 

答えて

12

あなたのユーザーを管理するために別のコントローラを作成する必要があります


は、私は、標準CRUDのための私のユーザーのコントローラを設定しています。私はいつも、管理者ユーザーを作成してで動作するように特別な名前空間を与え、私はそれを説明してみましょう:あなたはあなたのroutes.rbをして

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

をこの設定を追加する必要がある

config/routes.rb

devise :users # Allow users to register here 

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

Duh!それは私が探していた秘密のソースでした。非常に多くの問題を解決する...私は壁に私の頭をバッシュ行く間、私を許してください。ありがとう。 – neezer

+0

コントローラに名前空間を与える方法は? –

+0

Railsで名前空間を使用する方法についてもう少し詳しく説明します。app> controllersにフォルダを作成する必要があります。この例では、新しいフォルダadminをコントローラフォルダに置き、新しいユーザコントローラをそこに配置します。このファイルは、applcom/admin/usersからアクセスできます。http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing –

0

このような独自の登録コントローラを実装する

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

    ## other actions 
end 

次に、独自のビューを作成することができます オーバーライドdeviseのデフォルトコントローラの場合は、多分あなたはいくつかの機能を失ってしまいます。検証。自分で実装する必要があります

関連する問題