加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails 3.1在测试环境中设置主机

发布时间:2020-12-17 02:37:26 所属栏目:百科 来源:网络整理
导读:每当我在测试中使用root_url时,我都会收到http://www.example.com. 它在开发中工作正常,我在config / environments / development.rb中有这个: Rails.application.routes.default_url_options[:host]= 'localhost:3000' 但是,添加它在config / environments
每当我在测试中使用root_url时,我都会收到http://www.example.com.

它在开发中工作正常,我在config / environments / development.rb中有这个:

Rails.application.routes.default_url_options[:host]= 'localhost:3000'

但是,添加它在config / environments / test.rb中不起作用.我应该添加什么来使用localhost:3000作为测试环境中的主机?

解决方法

测试依赖于default_url_options的代码会导致各种问题,例如,参见 this thread和 issue.

我通过在测试中修补ActionDispatch :: Routing :: RouteSet来解决这个问题,以强制rails包含我想要的任何选项的默认值(在我的情况下是locale).有关详细信息,请参阅上面链接的github issue中的答案.

要使用相同的方法覆盖主机选项:

class ActionDispatch::Routing::RouteSet
  def url_for_with_host_fix(options)
    url_for_without_host_fix(options.merge(:host => 'localhost:3000'))
  end

  alias_method_chain :url_for,:host_fix
end

把它放在支持的文件中,应该做的伎俩.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读