2017-05-31 24 views
0

我有一個與設計相關的用戶模型。用戶信息模型屬於用戶模型。用戶信息模型通過表單獲取一些信息(姓名,電子郵件,學校等)。現在,只要用戶註冊或登錄,程序會檢查current_user是否填寫了信息,如果沒有,他們將被路由填寫信息頁面。如果他們填寫了信息,他們將被路由到主頁,這是userinfo索引頁面。索引頁顯示網站上所有用戶的用戶信息。如何在用戶登錄後立即將用戶發送到他們的個人資料?

的方式路由的工作原理:

USERINFO型號:

class Userinfo < ActiveRecord::Base 
    belongs_to :user 
    has_many :videos, through: :user 

    def info_complete? 
     name? && email? && college? && gpa? && major? 
    end 
end 

應用控制器:

def after_sign_in_path_for(resource) 
    if resource.userinfo.info_complete? 
     root_path 
    else 
     new_user_info_path 
    end 
    end 

問題

當用戶跡象,如果用戶有用戶信息填寫,我想要它轉到用戶的配置文件頁面,而不是索引或根路徑。配置文件路徑是「userinfo#show」。對於用戶信息#索引,不需要id,而是轉到「userinfo#show」,並且需要id。因爲userinfo#索引是'/ userinfos',而userinfos#show是'/ userinfos/userinfo_id'。我的問題是,如何將用戶路由到userinfo#show,個人資料頁面,只要他們登錄並且用戶信息數據已填寫完畢?

這可能很簡單,但我覺得我失去了一些東西。如何訪問應用程序控制器中的當前userinfo_id?所以我可以像這樣路線:

def after_sign_in_path_for(resource) 
    if resource.userinfo.info_complete? 
     userinfo_path(current_user.userinfo_id) 
    else 
     new_user_info_path 
    end 
    end 

或者類似上面的東西?

回答

1

正如在回答這個問題 Dynamic path helpers rails

def after_sign_in_path_for(resource) 
    if resource.userinfo.info_complete? 
    user_info_path(current_user.userinfo_id) 
    else 
    new_user_info_path 
    end 
end 
相關問題