2

I'm trying to follow this tutorial here but the tutorial seems to use this "match" function.

match '/about',   :to => 'pages#about'

Whenever, I do the same, I get this error from the server:

undefined method `match' for main:Object

How can I edit the routes.rb file such that:

  1. it will route from a long file path to a short one (eg. /pages/about to /about)
  2. I can have a "about_path" variable that I can link to ( eg: <%= link_to "About", about_path %> )
Steve
  • 11,831
  • 14
  • 51
  • 63

3 Answers3

4

Are you using Ruby on Rails 3? The match router syntax is for Rails 3 only. For previous versions you can define a named route:

map.about '/about', :controller => 'pages', :action => 'about'
John Topley
  • 113,588
  • 46
  • 195
  • 237
  • Apparently, I'm not using RoR 3, but I did try your method and it will shorten the path if I do map.about 'about', ... However, will this create a "about_path" that I can refer to globally from any page? – Steve Jul 20 '10 at 15:16
  • Yes, it will provide an `about_path` helper. – John Topley Jul 20 '10 at 15:21
0

I did it with:

  map.connect '/page', :controller => 'page2', :action => 'index'

so, in the URL I just redirect to http://x.x.x.x/page and RoR is really loading http://x.x.x.x/page2/index.html.erb

Best Regards,

Iván Carrasco Q.

0

that syntax is for the upcoming Rails3 (actually in beta4 but it's yet adopted for production, if you know what you're doing :P)

you should use this for rails 2.3:

map.about '/about', :controller => 'pages', :action => 'about'

this works if you have an action called 'about' that renders a specific page. otherwise, if 'about' is a simple page that you fetch from a 'show' action, passing an ID or a PERMALINK (eg: you're using permalink_fu plugin), then the right syntax is:

map.about '/about', :controller => 'pages', :action => 'about', :id => 'page_id_or_permalink'

this solution it's not the best: if you change the permalink or delete/re-create the page with a different id, then you must update the routes. by the way it works as you asked.

Andrea Pavoni
  • 5,311
  • 2
  • 29
  • 44