0

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.

Buddy Lindsey
  • 3,560
  • 6
  • 30
  • 42

2 Answers2

2

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".

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • 1
    Yeah I understand normally should do it via querystring, but I have a need for doing it this way. The code kind of works it breaks the edit_show_path helper, but not the new_show_path. Is that because of the :id constraint of the resources shows line? – Buddy Lindsey Feb 28 '11 at 00:41
  • Does manually visiting an edit path work (e.g. `/edit/1`)? Or is it just the path generation helper that's broken? – Andrew Marshall Feb 28 '11 at 00:45
  • Sorry I'm unsure of how to go about solving that, mixing and matching RESTful and non-RESTful routes isn't really the convention so it's not entirely surprising that the path helpers are confused. It's not the best, but you can fallback on `url_for`. You may also wish to run `rake routes` to see how Rails is actually interpreting your routes. – Andrew Marshall Feb 28 '11 at 01:00
  • Thanks, I'll keep messing with it to see if I can't figure it out some how. If I need to I guess I could always do it with query strings and just setup a custom .htaccess file. – Buddy Lindsey Feb 28 '11 at 01:06
  • This is the solution I just had bad code in other places. I also had some bad routing tests as well. Need to work on writing better tests, too. This was a mix of bad view code and bad tests. When I stripped all that away and went at it fresh everything worked. – Buddy Lindsey Feb 28 '11 at 02:04
0

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.

BJ Safdie
  • 3,399
  • 23
  • 23