2008年5月6日 星期二

rails routing

config/route.rb

set the page of webSite root:
map.root  :controller => "test"
note: remember to remove  /public/index.html

default route
map.connect  ':controller/:action/:id'
map.connect  ':controller/:action/:id.:format'
ex: 
http://localhost:3000/test/apple/3.xml
test map to :controller,  apple map to :action, 3 map to :id, xml map to :format
the parameter get from default route:
params[:controller],  params[:action],  params[:id]

set controller & action:
map.connect  'test/:var',  :controller=>rabbit,  :action=>jump
ex: 
http://localhost:3000/test/apple
apple map to :var

:requirements
set the requirement the component must match
ex:
map.connect  ":day",  :requirements => { :day => /[0-3]?\d }

the pattern for remaining components in the URL ( not specify in the URL)
use * with a name
ex:
map.connect  "*anything",  :action=> "unknown_action"

named route:
replace map.connect with map.routeName,
replace  url_for with routeName_url
ex:
 map.run  :controller=>"test", :action=>"run"
 redirect_to (run_url)
 link_to(" run" , run_url )




沒有留言: