A custom route along with resource routes

592 views Asked by At

I have setup a custom route, and it seems to work. However, I also have a resources routes as well for the same controller. I think I am just doing something wrong, but I can't tell what it is. I am honestly hacking together routes since I am still a bit confused on how to set them up and when to use what method.

Here are my routes I am dealing with right now.

resources :shows
match "shows/:country" => "shows#index"

The routes like the are the resources :shows works just fine, but not the match. If I flip them the match route works fine, but the resources :shows doesn't.

Should I do this as a namespaced route? I am not exactly sure what I should do. What I am trying to accomplish is something like this.

http://site.com/shows/canada

That will return all Candian shows.

Any help is appreciated.

2

There are 2 answers

5
Andrew Marshall On BEST ANSWER

What you probably want to do is use constraints, or maybe even a custom constraints class. Here's a rough start that I haven't tested and am unsure if it would work:

resources :shows, :constraints => { :id => /[0-9]+/ }
match "shows/:country" => "shows#index", :constraints => { :country => /[a-z]+/ }

Note that typically this would be done via a get query parameter, e.g. http://example.com/shows?country=canada, which would already go to your shows#index action and have params[:country] set to "canada".

1
BJ Safdie On

You may be getting bitten by the default route which expects /{controller}/{action} and routes accordingly. Try removing the default route. You will have to make sure to declare all of your routes, but the result is a more predictable set of routes for your app.