在Rails中,如果已将对象声明为 route 的资源,则可以将其传递给redirect_to
:
redirect_to @post
我有一个属于所有者的多态对象
Attachment
。 @attachment.owner
可以是任何对象。我需要重定向到附件的所有者,但是附件及其所有者都是管理 namespace 中的资源:namespace :admin do
resources :attachments, :posts, :comments #etc
end
如果我知道
@attachment.owner
是一个帖子,则可以redirect_to admin_post_path(@attachment.owner)
,但是由于它可以是任何对象,我该如何进行重定向?
最佳答案
您可以使用 polymorphic_path
polymorphic_path([:admin, @attachment.owner])
关于ruby-on-rails - admin namespace 中的redirect_to对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10865076/