我可以做 STI 并仍然使用多态路径助手吗? [英] Can I do STI and still use polymorphic path helpers?

查看:42
本文介绍了我可以做 STI 并仍然使用多态路径助手吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用单表继承并对所有子类进行评论.我只对所有不同的 STI 类型使用 1 个控制器.当 form_for 助手为子类型生成 URL 时,它尝试为子类型使用助手,但我希望它为父类型使用助手.

I am using Single Table Inheritance and have comments on all the subclasses. I am only using 1 controller for all the different STI types. When the form_for helper generates a URL for a subtype it tries to use a helper for the subtype, but I want it to use the helper for the parent.

这是我得到的错误:

undefined method `subclasstypename_comments_path' for #<ActionView::Base:0x41ef27c>

它应该"使用的路径助手是

The path helper it 'should' use is

parentclasstypename_comments_path

推荐答案

是的,只需使用 AR::Base#becomes.

假设您的基类是 Account,它是 GuestAccountLoginAccount 的子类.

Say your base class is Account, which is subclassed to GuestAccount and LoginAccount.

@account.is_a? LoginAccount? #=> true

然后你可以做一个

form_for [@account.becomes(Account), @comment] do |f|
  ...

这篇关于我可以做 STI 并仍然使用多态路径助手吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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