单表继承路由?
我的单表继承对我的应用程序工作得很好。我有两个用户子类型(运动员和公司)继承了超级用户。
I have single table inheritance working just fine for my app. I have two user subtypes (Athlete and Company) that inherit the super-type User.
假设我列出了所有用户,并希望从此列表中链接到每个用户的个人资料。我想链接到运动员控制器,如果类型是运动员,公司控制器,如果类型是公司。是否有标准的Rails方式?也许一些路由技巧?
Let's say I am listing all users, and want a link to each user's profile from this list. I want to link to the athletes controller if the type is athlete and the companies controller if the type is company. Is there a standard Rails way to this? Maybe some routing tricks?
你甚至可以做得那么简单,Rails会识别它必须处理哪种类型的用户,所以假设你有实例变量@user wich可以是运动员或公司,你可以这样做
you can even do that much simpler, Rails recognizes which type of user it has to deal with, so let's say you have the instance variable @user wich can either be an Athlete or a Company, you can just do that
= link_to "Profile", @user
BAM! Rails魔术!
BAM! Rails magic!