Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect_to object in admin namespace

In Rails you can pass an object to redirect_to if you've declared it as a resource in your routes:

redirect_to @post

I have a polymorphic object Attachment that belongs to an owner. @attachment.owner could be any object. I need to redirect to the attachment's owner, but both attachment and its owner are resources within an admin namespace:

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

If I knew the @attachment.owner was a post, I could redirect_to admin_post_path(@attachment.owner), but since it could be any object, how to I do the redirect?

like image 608
tybro0103 Avatar asked Jun 02 '12 19:06

tybro0103


1 Answers

You can use polymorphic_path

polymorphic_path([:admin, @attachment.owner])
like image 79
Flexoid Avatar answered Oct 01 '22 14:10

Flexoid