views:

110

answers:

2

I want http://localhost:3000/note-1828 to map to a controller action. I've tried this:

  map.connect "note-:id",
    :controller => "annotations",
    :action => "show",
    :requirements => { :id => /\d+/ }

But it doesn't seem to work (No route matches "/note-1828" with {:method=>:get}). What should I do instead?

+2  A: 

Route variables (like :id) can only happen in between path separators, in this case, slashes.

Your best bet would be RESTing your routes, to use /notes/:id instead.

But, probably, you're rewriting an existing site and want to keep your URLs. In this case I'd use .htaccess and mod_rewrite to reroute like this:

RewriteRule note-(\d+) /notes/$1 [R=301]

(the .htaccess must obviously be in the /public directory)

Leonid Shevtsov
+2  A: 

Agree with Leonid. And in case you just want to make some "pretty" urls, you can apply the to_param method to your notes model. Thus:

def to_param
  "#{id}-notes"
end

And adding a RESTful route:

map.resources :annotations, :as => "notes"

Would gave you something like:

http://yourdomain.com/annotations/1828-notes

If you don't want the "notes" part, you can map a route to something like

map.connect "/:id", :controller=>"annotations", :action=>"show"

and getting

http://yourdomain.com/1828-notes
Yaraher