修改routes.rb,路由优先级从上到下逐行匹配,最下面是默认路由,修改后重启服务
1,自定义路由
match 'controllerA/action1' => 'controllerB#action2' (或 match 'controllerA/action1', :to => 'controllerB#action2')
#将controllerA的action1路由到controllerB的action2,地址栏url不跳转,仅仅网页内容做切换
2,命名路由
match '/meetings' => 'events#index', :as => "meetings"
会产生mettings_path和meetings_url的helper,前者产生相对路径,后者产生绝对路径
3,match '/:username' => 'users#show'
#controller中可以通过params[:username]获取 !!!
如http://127.0.0.1/ciaos会跳转到http://127.0.0.1/user/show(username=ciaos)
4,默认网站根目录,必须同时删除publick/index.html
配置网站根目录 root :to => "democontroller#index"
5,命名空间Namespace,我們可以在URL網址前多加一段,特別適合例如後台介面:
#如rails generate controller admin/main index login logout namespace :admin do get "main/index" get "main/login" get "main/logout" end #访问127.0.0.1/admin/main/index
6,默认路由
match ':controller(/:action(/:id))(.:format)'
7,在项目根目录下执行rake routes查看所有路由
参照 :
1,http://ihower.tw/rails3/routing.html
2,http://guides.ruby-china.org/routing.html