2011-08-26 65 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

修改了答案。 –

相关问题