2017-02-01 12 views
0

私はルートをdevise/sessionsにしたいと思う私のレールアプリのroot_pathを設定したい#ユーザがサインインしていない場合はnew とhome#index if user私はuser_signed_inを使用することはできませんか?ログインしているかどうかに基づいてユーザーをリダイレクトする

私はuser_signed_inを使用できません。方法で私はそれを達成することができますか? また、devise/sessions#newルートパスを作成する方法はありますか?

答えて

2

あなたのapplication_controller.rb`にこのような何かを行うことができます。

devise_for :users 

devise_scope :user do 
    authenticated :user do 
    root 'home#index', as: :authenticated_root 
    end 

    unauthenticated do 
    root 'devise/sessions#new', as: :unauthenticated_root 
    end 
end 

更新:あなたのapplication_controller.rb

before_action :authenticate_user! 

def after_sign_in_path_for(resource_or_scope) 
# your_path 
end 
+0

これは私のエラーを与えるが、工夫のためのマッピングを見つけることができませんでした「/」私の答えを更新しました –

+0

@AlaapDhallは、私が – Hizqeel

+0

を教えていない場合、それは今役に立てば幸いこれは私が持っているエラーメッセージです..そして、私は範囲内ですべてを持っていますが、ルータの事をバイパスしていると確信していません。 –

0

あなたが行うことができます

Rails.application.routes.draw do 
    devise_for :users 

    unauthenticated do 
    root to: 'home#index', as: 'unauthenticated' 
    end 

    authenticated :user do 
    root to: 'sessions#new', as: 'authenticated' 
    end 
end 
1

それは非常にエレガントではないのですが、あなたは自分の家の#インデックスのcontroler

DEFインデックス user_signed_in場合を編集することができますか? ...あなたのコード 他 redirect_toのnew_user_session_path 終了 エンド

関連する問題