单表继承路由? [英] Single Table Inheritance routing?

查看:122
本文介绍了单表继承路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单表继承对我的应用程序工作得很好。我有两个用户子类型(运动员和公司)继承了超级用户。

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!

这篇关于单表继承路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆