Rails——单表继承和路由

发布于 2024-11-30 17:10:09 字数 337 浏览 1 评论 0原文

我正在实现单表继承,但在路由方面遇到一些问题。我有两个类,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-12-07 17:10:09

在控制器中使用继承。在 AlphaController 类中实现编辑和更新功能,然后在 BetaControllerGammaController 类中继承该类,然后在其中实现您的其他功能。

class AlphaController < ActionController::Base

   def edit
      ...
   end

   def update
      ...
   end

end

class BetaController < AlphaController

   def index
      ...
   end

end

请注意,这将使您的 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 you BetaController and GammaController classes, where you then implement your other functions.

class AlphaController < ActionController::Base

   def edit
      ...
   end

   def update
      ...
   end

end

class BetaController < AlphaController

   def index
      ...
   end

end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文