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?
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?
Here is how I do it in any context without
include
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 worksand this does not work
One more example with Capybara tests
and now the right one
Delegation to url_helpers seems much better than including the whole module into your model
reference
I've been struggling with the niceties the helper is expecting from the default controller and stack (
default_url_options
, etc.), and didn't want to hardcode the host.Our URL helpers are provided by our nifty module, of course:
But include this as is, and (1) the helper is going to look for
default_url_options
, and (2) won't know about the request host nor the request.The host part comes from the controller instance's
url_options
. Hence, I pass the controller context into my former module, now a class:Might not fit every case, but it's been useful to me when implementing a sort of custom renderer.
In any way:
to Augustin Riedinger, that delegation code needs to refer to url_helpers (plural), otherwise you get
In your module, just perform a :