我可以做STI并仍然使用多态路径助手吗?

Kyl*_*oon 12 ruby ruby-on-rails

我正在使用单表继承并对所有子类都有注释.我只为所有不同的STI类型使用1个控制器.当form_for帮助程序为子类型生成URL时,它会尝试使用子类型的帮助程序,但我希望它使用父类的帮助程序.

这是我得到的错误:

undefined method `subclasstypename_comments_path' for #<ActionView::Base:0x41ef27c>
Run Code Online (Sandbox Code Playgroud)

它应该使用的路径助手是

parentclasstypename_comments_path
Run Code Online (Sandbox Code Playgroud)

ben*_*n_h 57

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

说你的基类是Account,子类是GuestAccountLoginAccount.

@account.is_a? LoginAccount? #=> true
Run Code Online (Sandbox Code Playgroud)

然后你可以做一个

form_for [@account.becomes(Account), @comment] do |f|
  ...
Run Code Online (Sandbox Code Playgroud)