【问题标题】:How to add extra named routes definitions in Rails 4.2?如何在 Rails 4.2 中添加额外的命名路由定义?
【发布时间】:2021-07-09 03:17:37
【问题描述】:

我正在将 RoR 应用程序从 4.0 升级到 4.2。 在 4.0 中,我们有 url_helpers,它添加​​了额外的 url_helpers,如下所示:

module UrlHelper
  def subscribe_url (tenant)
    url_options = {
       :controller => "users/subscription",
       :action     => "new",
       :only_path  => false,
       :subdomain  => tenant.sitename
    }
    url_for url_options
  end
end

那么我们的 base_controller 将只包含它们:

class BaseController < ActionController::Base
  # Gives us access to custom resource paths and overrides url_for
  # to provide support for multitenancy.
  include UrlHelper

但是,在更新到 Rails 4.2.11.3 后,它停止了工作。 我们的测试似乎无法再访问 subscribe_url,即使它包含在 base_controller 中。

expect(response).to redirect_to subscribe_url(tenant: tenant)

失败:

undefined method `login_url' for #<RSpec::ExampleGroups::UsersRegistrationsController

有没有其他方法可以将 url_helpers 添加到 rspec 测试?

很遗憾,不能使用 direct 路由,因为它还不是 Rails 5。

【问题讨论】:

  • 附带说明 - 切勿在 ruby​​ 中的方法名称和父项之间添加空格。由于父母是可选的,它被视为def subscribe_url((tenant)),添加任何额外的参数都会导致语法错误,因为它实际上不是一个列表。

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

在定义了辅助方法后,您必须重新启动服务器。点击here!

【讨论】:

  • 哪个服务器?我无法在 rspec 中访问它们
猜你喜欢
  • 2012-02-23
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多