简体   繁体   中英

Overriding URL helpers in Rails

I have a Model ( Show ) in Rails that is accessed via a subdomain rather than a standard REST URL. In the file app/helpers/url_helper.rb I have the following method:

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 . In integration tests, however, the method doesn't work, and the default one generated by rails is used instead. If I run puts show_url(@show) there, I just get http://example.com . How do I use this custom URL helper in my integration tests?

Edit:

routes.rb section regarding this subdomain stuff:

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 .

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

Or another way:

resources :show, only: %w(index new create edit update delete)

我真的会考虑做一些重构和重命名Show模型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM