2011-08-26 61 views
0

我有Rails型號UserReadingListSessionReadingList。用戶有許多閱讀列表。 A SessionReadingList是一種特殊類型的閱讀列表,用於在用戶註冊之前存儲在會話中。這是繼承的情況嗎?

在我ReadingListsController每一個動作的形式爲:

def show 
    if current_user 
    #load user's reading lists 
    else 
    #load session reading list from session 
    end 
end 

我不知道我是否會更好繼承ReadingListsController所以我有例如SessionReadingListsControllerUserReadingListsController。不過,我不知道如何處理路由。

那麼,解決方案的子類?如果是這樣,我是否根據current_userReadingListsController重定向?或者,還有更好的方法?

回答

0

您可以創建使用適當控制器的自定義路由匹配器。

class LoggedInConstraint < Struct.new(:value) 
    def matches?(request) 
    request.cookies.key?("user_token") == value 
    end 
end 

match 'reading-list' :to => "reading_list#index", :constraints => LoggedInConstraint.new(true) 
match 'reading-list' :to => "session_reading_list#index", :constraints => LoggedInConstraint.new(true) 
+0

問題是還有其他條件邏輯,例如向用戶添加新讀取列表與將其添加到會話中不同。 – Skilldrick

+0

修改了答案。 –

相關問題