Ruby-on-rails 如何从 rails 模块访问 URL helper
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6074831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access URL helper from rails module
提问by sizzle
I have a module with a function. It resides in /lib/contact.rb:
我有一个带有功能的模块。它驻留在 /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?
我想访问模块内的“users_path”等 URL 帮助程序。我怎么做?
回答by ronnieonrails
In your module, just perform a :
在您的模块中,只需执行:
include Rails.application.routes.url_helpers
回答by Naveed
回答by Anton Chikin
Here is how I do it in any context without include
这是我在任何情况下都没有做到的方法 include
routes = Rails.application.routes.url_helpers
url = routes.some_path
That works in any context. If you're trying to includeurl_helpers - make sure you are doing that in the right place e.g. this works
这在任何情况下都有效。如果您正在尝试使用includeurl_helpers - 确保您在正确的位置执行此操作,例如这有效
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
Capybara 测试的另一个例子
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
回答by Jonathan Allard
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.
我一直在努力解决助手对默认控制器和堆栈(default_url_options等)的期望,并且不想对主机进行硬编码。
Our URL helpers are provided by our nifty module, of course:
我们的 URL 助手由我们的漂亮模块提供,当然:
include Rails.application.routes.url_helpers
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.
但是按原样包含它,并且 (1) 助手将查找default_url_options,并且 (2) 不会知道请求主机或请求。
The host part comes from the controller instance's url_options. Hence, I pass the controller context into my former module, now a class:
主机部分来自控制器实例的url_options. 因此,我将控制器上下文传递到我以前的模块中,现在是一个类:
class ApplicationController
def do_nifty_things
HasAccessToRoutes.new(self).render
end
end
class HasAccessToRoutes
include Rails.application.routes.url_helpers
delegate :default_url_options, :url_options, to: :@context
def initialize(context)
@context = context
end
def render
nifty_things_url
end
end
Might not fit every case, but it's been useful to me when implementing a sort of custom renderer.
可能不适合所有情况,但在实现某种自定义渲染器时对我很有用。
In any way:
以任何方式:
- if you want access to the default url options seamlessly, or the host of the request, you need to pass controller/request context in
- if you just need the path, no host, and don't care about the url options, you can just make some dummy methods.
- 如果您想无缝访问默认 url 选项或请求的主机,则需要将控制器/请求上下文传递给
- 如果您只需要路径,不需要主机,并且不关心 url 选项,则可以制作一些虚拟方法。
回答by Jerome
delegate :url_helpers, to: 'Rails.application.routes'
url_helpers.users_url => 'www.foo.com/users'
to Augustin Riedinger, that delegation code needs to refer to url_helpers (plural), otherwise you get
对于 Augustin Riedinger,该委托代码需要引用 url_helpers(复数),否则您会得到
undefined method `url_helper'
未定义的方法`url_helper'

