2012-04-28 8 views
2

私は登録用Deviseを使用するRailsアプリケーションを開発しています。私はユーザの役割を管理するために単一テーブルの継承を使用します。similar to this私は3つのユーザモデルUser < ActiveRecord,Admin < UserおよびCollaborator < Userを持っています。コラボレーターと管理者はセッションを共有します。私の現在の問題は、新しいユーザーを作成することです。ユーザーはデータベースに保存され、user_signed_inに保存されますか? trueを返しますが、何らかの理由でcurrent_userがnilを返します。新規ユーザー、Rails 3.2を作成すると、devise current_userがnilを返します

ユーザーが自分のアカウントを作成するとき、それはこのようになります私のAccountsControllerのindexアクションにリダイレクトする必要があります:私は代わりにサインインしようとすると、同じコードが動作する

undefined method `accounts' for nil:NilClass 

:中

def index 
    @accounts = current_user.accounts 
end 

結果。

私のルートは、この(as of)ようになります

# New Version using dynamic methods 
    %w(Collaborator Admin).each do |k| 
    define_method "current_#{k.underscore}" do 
    current_user if current_user.is_a?(k.constantize) 
    end 

    define_method "authenticate_#{k.underscore}!" do |opts={}| 
    send("current_#{k.underscore}") || not_authorized 
    end 
end 

def after_sign_in_path_for(resource) 
    if resource.is_a?(User) 
    accounts_path 
    else 
    super 
    end 
end 

def after_sign_up_path_for(resource) 
    accounts_path 
end 

誰もがこれを引き起こしているものを知っています:3210

devise_for :users, :controllers => {:sessions => 'sessions'}, :skip => [:registrations] do 
    delete '/logout', :to => 'sessions#destroy', :as => :destroy_user_session 
    get '/sign_in', :to => 'sessions#new', :as => :new_user_session 
    post '/sign_in', :to => 'sessions#create', :as => :user_session 
end 
devise_for :admins, :controllers => {:registrations => 'admins/registrations'}, :skip => :sessions do 
    get '/sign_up', :to => 'admins/registrations#new', :as => :new_admin 
    post '/sign_up', :to => 'admins/registrations#create', :as => :admin 
end 
devise_for :collaborators, :controllers => {:registrations => 'collaborators/registrations'}, :skip => :sessions 

はまた、私は(the same as of)を使用するいくつかのヘルパーメソッドを作成していますか?

答えて

0

私はこのように、after_sign_up_path_forカスタマイズすることによって、それを解決するために管理:

def after_sign_up_path_for(resource) 
    sign_in(resource) 
    accounts_path 
end 

それを行うには良い方法があるなら、私に教えてください。

+0

これを解明したことがありますか?私はそれが数年たっていることを知っていますが、同様の状況に陥っており、修正プログラムの使用を避けたいと考えています。シンプルで機能しますが、そうする必要はないはずです – MingMan

関連する問題