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?
You can use polymorphic_path
polymorphic_path([:admin, @attachment.owner])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With