ruby-on-rails - admin namespace 中的redirect_to对象

标签 ruby-on-rails ruby-on-rails-3 redirect routes

在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/

相关文章:

ruby-on-rails - 无法运行 Minitest - 在 MiniTest::Unit::TestCase 中获取未定义的类变量

ruby-on-rails - 两列上的单独索引和 Rails 中对的唯一约束

ruby-on-rails - rails 3 : Where to store shared methods for models?

ruby-on-rails-3 - 导轨 : email confirmation RESTful (PUT) URL in mailer

regex - 带有通配符和问号的 Apache Rewrite 问题

javascript - Ruby on Rails : How to generate an array from records to display in Highcharts?

javascript - 使用来自 javascript 的 turbolinks 重定向

ruby-on-rails - 从 Rails + Postfix 发送的电子邮件进入垃圾邮件文件夹

tomcat - 为什么 Tomcat 管理器重定向到本地主机 :8080/company/www. company.org/index.html

ruby-on-rails - Ruby on Rails 重定向 www.到非 www 版本的网站