Overriding URL helpers in Rails

560 Views Asked by At

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.

2

There are 2 best solutions below

8
On

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", e.g.

Or another way:

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

I would really consider doing some refactoring and renaming the Show model.