【问题标题】:How to access URL helper from rails module如何从 rails 模块访问 URL 助手
【发布时间】:2011-08-29 19:27:26
【问题描述】:

我有一个带有功能的模块。它位于 /lib/contact.rb:

module Contact
  class << self
    def run(current_user)
      ...
    end
  end
end

我想访问模块内的 URL 帮助程序,例如“users_path”。我怎么做?

【问题讨论】:

标签: ruby-on-rails ruby url model helper


【解决方案1】:

我一直在努力解决助手对默认控制器和堆栈(default_url_options 等)的期望,并且不想对主机进行硬编码。

我们的 URL 助手当然是由我们漂亮的模块提供的:

include Rails.application.routes.url_helpers

但按原样包含此内容,并且 (1) 助手将查找 default_url_options,并且 (2) 不会知道请求主机或请求。

主机部分来自控制器实例的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

可能不适合所有情况,但在实现某种自定义渲染器时它对我很有用。

无论如何:

  • 如果你想无缝访问默认的url选项,或者请求的主机,你需要传入控制器/请求上下文
  • 如果你只需要路径,不需要主机,并且不关心 url 选项,你可以做一些虚拟方法。

【讨论】:

    【解决方案2】:

    委托给 url_helpers 似乎比将整个模块包含到模型中要好得多

    delegate :url_helpers, to: 'Rails.application.routes' 
    url_helpers.users_url  => 'www.foo.com/users'
    

    reference

    【讨论】:

    • 我认为应该是 url_helpers 而不是 url_helper
    • 您好,您怎么知道应该将 url_helpers 委托给 Rails.application.routes?您在哪里可以找到此文档?谢谢!
    • @NikSo api.rubyonrails.org 是开始探索的好地方。这是 URL 助手的文档api.rubyonrails.org/classes/ActionController/UrlFor.html 包括整个模块似乎有点过分,委托是更好的选择,但还是个人意见:)
    【解决方案3】:
    delegate :url_helpers, to: 'Rails.application.routes' 
    url_helpers.users_url  => 'www.foo.com/users'
    

    对 Augustin Riedinger,委托代码需要引用 url_helpers(复数),否则你会得到 ​​p>

    未定义的方法`url_helper'

    【讨论】:

      【解决方案4】:

      这是我在没有include 的任何情况下的做法

      routes = Rails.application.routes.url_helpers
      url = routes.some_path
      

      这在任何情况下都有效。如果您尝试include url_helpers - 请确保您在正确的位置执行此操作,例如这行得通

      module Contact
        class << self
          include Rails.application.routes.url_helpers
        end
      end
      

      这不起作用

      module Contact
        include Rails.application.routes.url_helpers
        class << self
        end
      end
      

      Capybara 测试的另一个示例

      feature 'bla-bla' do
        include Rails.application.routes.url_helpers
        path = some_path #unknown local variable some_path
      end
      

      现在是正确的

      include Rails.application.routes.url_helpers
      feature 'bla-bla' do
        path = some_path #this is ok
      end
      

      【讨论】:

        【解决方案5】:

        在您的模块中,只需执行:

         include Rails.application.routes.url_helpers
        

        【讨论】:

        • 对我不起作用... :(你能提供更多细节吗?
        • 应该注意的是,在 Rails 应用程序内的单独类中包含该操作将擦除现有的已初始化 url_helpers,从而导致错误:缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或设置 :only_path 为 true
        • 如果只需要一两次,可以直接内联使用,如Rails.application.routes.url_helpers.users_path。我发现这对阅读您的代码的其他人更明确和有帮助。
        猜你喜欢
        • 1970-01-01
        • 2010-09-30
        • 2011-06-27
        • 1970-01-01
        • 1970-01-01
        • 2011-09-16
        • 2012-10-27
        • 2012-12-07
        • 2014-11-07
        相关资源
        最近更新 更多