【问题标题】:redirect_to object in admin namespace管理命名空间中的 redirect_to 对象
【发布时间】:2012-06-07 13:38:15
【问题描述】:

在 Rails 中,如果您已将对象声明为路由中的资源,则可以将对象传递给 redirect_to

redirect_to @post

我有一个属于所有者的多态对象Attachment@attachment.owner 可以是任何对象。我需要重定向到附件的所有者,但附件及其所有者都是管理命名空间中的资源:

namespace :admin do
  resources :attachments, :posts, :comments #etc
end

如果我知道@attachment.owner 是一个帖子,我可以redirect_to admin_post_path(@attachment.owner),但既然它可以是任何对象,我该如何进行重定向?

【问题讨论】:

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


    【解决方案1】:

    您可以使用polymorphic_path

    polymorphic_path([:admin, @attachment.owner])
    

    【讨论】:

    • redirect_to [:admin, @attachment.owner] 成功了。谢谢!
    【解决方案2】:

    redirect_to 将符号作为参数。定义一个具有相同名称的私有方法并在该方法中评估 url。

    redirect_to :my_owner
    
    private
    
    define my_owner
      <evaluate the path here>
    end
    

    【讨论】:

    • 我的目标是 - 不 - 自己评估路径。
    猜你喜欢
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多