Access polymorphic_path inside a model in Rails
It's not a good separation of concerns, but sometimes could be useful to access routes inside a model. Simply add to your model:
include Rails.application.routes.url_helpers
If you also need access to polymorphic routes:
include ActionDispatch::Routing::PolymorphicRoutes
include Rails.application.routes.url_helpers
def link_to_self
polymorphic_path [self.parent, self]
end
Written by Marc Anguera Insa
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#