1

My problem is i want to remove the querystring part of this URL and make it clean.

http://staging.mysite.com/mycontroller?name=/philippines/about-manila

currently i have MyController.index() and I parse the name parameter in this method.

What i would eventually want is this :

http://staging.mysite.com/mycontroller/philippines/about-manila

the parameter part 'philippines/about-manila' can have arbitrary number of parameters, like

http://staging.mysite.com/mycontroller/philippines/about-manila/people

How can i do this in routes?

Zimbabao
  • 8,150
  • 3
  • 29
  • 36
r2b2
  • 1,255
  • 5
  • 13
  • 34

3 Answers3

4

It sounds like you want route globbing. If you use:

map.my_route '/mycontroller/*parts',
             :controller => :mycontroller,
             :action => :index

and then go to the URL http://staging.mysite.com/mycontroller/philippines/about-manila/people, then your mycontroller controller's index action will be called, and params[:parts] will contain the array ["philippines", "about-manila", "people"].

Simon
  • 25,468
  • 44
  • 152
  • 266
2

Is it really arbitrary — or just variable?

If it's just variable, you can enclose optional parameters in parentheses:

match '/:city(/:section(/:subsection))', :controller => :mycontroller,
    :action => :index

For Rails 2.x:

map.connect '/:city(/:section(/:subsection))', :controller => :mycontroller,
    :action => :index
Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63
1

For Rails 2.x you can use

map.my_route '/:my_param', :controller => :mycontroller, :action => :index

Then in your controller you can access

params[:my_param]

If you want links just go with

my_route_path(:my_param => "mytekst")
Adrian Serafin
  • 7,665
  • 5
  • 46
  • 67