[英]Overriding URL helpers in Rails
I have a Model ( Show
) in Rails that is accessed via a subdomain rather than a standard REST URL. 我在Rails中有一个模型(
Show
),可以通过子域而不是标准REST URL进行访问。 In the file app/helpers/url_helper.rb
I have the following method: 在文件
app/helpers/url_helper.rb
我具有以下方法:
def show_url(show)
root_url(subdomain: show.subdomain)
end
In controllers, this works perfectly. 在控制器中,这很完美。 I can test it with
puts show_url(@show)
and it outputs the subdomain
of the show as expected: http://test.example.com
. 我可以使用
puts show_url(@show)
对其进行测试,并按预期方式输出显示的subdomain
: http://test.example.com
: puts show_url(@show)
。 In integration tests, however, the method doesn't work, and the default one generated by rails is used instead. 但是,在集成测试中,该方法不起作用,而是使用由rails生成的默认方法。 If I run
puts show_url(@show)
there, I just get http://example.com
. 如果我在这里运行
puts show_url(@show)
,我只会得到http://example.com
。 How do I use this custom URL helper in my integration tests? 如何在集成测试中使用此自定义URL助手?
Edit: 编辑:
routes.rb
section regarding this subdomain stuff: 有关此子域内容的
routes.rb
部分:
constraints(lambda do |request|
request.subdomain.present? && request.subdomain != 'www'
end) do
get '/' => 'shows#show', as: :show
get '/edit' => 'shows#edit', as: :edit_show
end
This is based loosely around a Railscast on subdomain matching . 这大致基于Railscast的子域匹配 。
Try defining its route without the default "show" action: 尝试在没有默认“显示”操作的情况下定义其路线:
# config/routes.rb
resources :show, except: :show
Sounds a bit confusing since your model is called Show
, but what it's doing is defining all the standard restful routes (index, new, create, edit, update, delete) except for "show", eg 由于您的模型称为
Show
,因此听起来有点混乱,但是它的作用是定义除“ show”之外的所有标准静态路由(索引,新建,创建,编辑,更新,删除),例如
Or another way: 或另一种方式:
resources :show, only: %w(index new create edit update delete)
我真的会考虑做一些重构和重命名Show模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.