Posted over 7 years ago. Visible to the public. Repeats.

How to use Rails URL helpers in any Ruby class

In Rails 3+, you can use:

Copy
class Project delegate :url_helpers, to: 'Rails.application.routes' def project_path url_helpers.project_path(self) end end

For Rails 2, use the attached Modularity trait. It will give any Ruby class a method #url_writer, on which you can call URL helpers:

Copy
class Foo does 'write_urls' def self.class_method url_writer.session_path end def instance_method url_writer.project_path(5) end end

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 2 years ago
by Henning Koch
Attachments:
write_urls_trait.rb
Keywords:
route, routes, from, model
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more