2011-05-01 11 views
5

新しいユーザーがDeviseから登録するときに、簡単な方法とアクションを追加する必要があります。Deviseに登録するコントローラをカスタマイズするにはどうすればよいですか?

私に電子メールを送るnotifyメソッドを適用したいと思います。

私はacts_as_networkを使用してセッション値を渡し、新しいレジスタを招待した人に接続したいと考えています。

どのようにカスタマイズするのですか、ドキュメントを見ましたが、私は何が必要なのか完全に明確ではありません....ありがとう!

+0

あなたは、この記事では見たことがありますか? http://stackoverflow.com/questions/3546289/override-devise-registrations-controller – mbreining

+0

@feelway、いいえ私はしませんでしたが、それは有望そうです...私は "スーパー"はDeviseコントローラから継承していると思いますか? – Angela

+0

はい。 Superは継承したメソッドのコードを実行します。 – dombesz

答えて

15

これはDevise Registrationsコントローラをオーバーライドするために私がやっていることです。新しいユーザーを登録するときにスローされる可能性のある例外をキャッチする必要がありましたが、同じテクニックを適用して登録ロジックをカスタマイズすることができます。

アプリ/コントローラ/工夫/カスタム/ registrations_controller.rb

class Devise::Custom::RegistrationsController < Devise::RegistrationsController 
    def new 
    super # no customization, simply call the devise implementation 
    end 

    def create 
    begin 
     super # this calls Devise::RegistrationsController#create 
    rescue MyApp::Error => e 
     e.errors.each { |error| resource.errors.add :base, error } 
     clean_up_passwords(resource) 
     respond_with_navigational(resource) { render_with_scope :new } 
    end 
    end 

    def update 
    super # no customization, simply call the devise implementation 
    end 

    protected 

    def after_sign_up_path_for(resource) 
    new_user_session_path 
    end 

    def after_inactive_sign_up_path_for(resource) 
    new_user_session_path 
    end 
end 

私はRegistrationsControllerの私のカスタマイズしたバージョンを配置した場所私はapp/controllersの下に新しいdevise/customディレクトリ構造を作成したノート。その結果、デベロッパ登録のビューをapp/views/devise/registrationsからapp/views/devise/custom/registrationsに移動する必要があります。

また、devise Registriesコントローラを無効にすると、登録が成功した後にユーザーをリダイレクトするなどのいくつかの他のものをカスタマイズできます。これは、after_sign_up_path_forおよび/またはafter_inactive_sign_up_path_forのメソッドをオーバーライドすることによって行われます。

routes.rbを

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

このpostは、あなたが興味があるかもしれない追加情報を提供することがあります。

関連する問題