I have a module with a function. It resides in /lib/contact.rb:
module Contact   class << self     def run(current_user)       ...     end   end end   I want to access the URL helpers like 'users_path' inside the module. How do I do that?
Module ActionView::Helpers::UrlHelper. Provides a set of methods for making links and getting URLs that depend on the routing subsystem (see ActionDispatch::Routing). This allows you to use the same format for links in views and controllers.
Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.
In your module, just perform a :
 include Rails.application.routes.url_helpers 
                        Here is how I do it in any context without include
routes = Rails.application.routes.url_helpers url = routes.some_path   That works in any context. If you're trying to include url_helpers - make sure you are doing that in the right place e.g. this works
module Contact   class << self     include Rails.application.routes.url_helpers   end end   and this does not work
module Contact   include Rails.application.routes.url_helpers   class << self   end end   One more example with Capybara tests
feature 'bla-bla' do   include Rails.application.routes.url_helpers   path = some_path #unknown local variable some_path end   and now the right one
include Rails.application.routes.url_helpers feature 'bla-bla' do   path = some_path #this is ok end 
                        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