Rails——单表继承和路由
我正在实现单表继承,但在路由方面遇到一些问题。我有两个类,Gamma 和 Beta,它们都继承自 Alpha。我知道,如果我想使用 Alpha 作为控制器而不是 Beta 和 Gamma 的单独控制器,我可以按照说明进行操作 此处用于将资源默认分配给另一个控制器。
然而,我想做的是让一些方法由中央 Alpha 控制器处理(例如编辑和更新),而其他方法由子类 Beta 和 Gamma 控制器处理。我如何指定哪些方法应指向 Alpha 控制器,哪些方法应由 Beta 和 Gamma 处理?
I a implementing single table inheritance but am having some trouble with the routing. I have two classes, Gamma and Beta, and both inherit from Alpha. I know that if I want to use Alpha as the controller as opposed to individual controllers for Beta and Gamma, I can follow the instructions here for having a resource default to another controller.
However, what I want to do is have some methods be handled by a central Alpha controller (e.g. edit and update), while other methods be handled by the subclass Beta and Gamma controllers. How can I specify which methods should be pointed to the Alpha controller and which should remain to be handled by Beta and Gamma?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在控制器中使用继承。在
AlphaController
类中实现编辑和更新功能,然后在BetaController
和GammaController
类中继承该类,然后在其中实现您的其他功能。请注意,这将使您的 URL .../beta/edit 和 .../beta/update。
您只想使用views/alpha/edit.html.erb作为部分视图,然后将alpha视图页面渲染为beta/gamma视图中的部分。
请记住,即使您将对象存储在一个表 Alpha 中,您的模型中仍然有两个类,Beta 和 Gamma,并且应该这样对待它们。
Use inheritance with your controllers. Implement your edit and update functions in an
AlphaController
class, and then inherit from that class in youBetaController
andGammaController
classes, where you then implement your other functions.Note that this will make your URLs .../beta/edit and .../beta/update.
You would only want to use the views/alpha/edit.html.erb as a partial view, and then render the alpha view page as a partial in your beta/gamma views.
Remember that even though you are storing the objects in one table Alpha, you still have two classes in your model, Beta and Gamma, and should treat them as such.