Rails routes
root 'static_pages#home'
這行的動作代表把根目錄指向給一個 controller 和一個 action,如此 Rails 就會建立具名路由,使用具體名稱指定路由,而不是原始的 URL 指定路由。具名路由會產生兩種,例如一種是 root_path,一種是 root_url,差別如下:
root_path -> '/'
root_url -> 'http://www.example.com/'
如果有需要重新導向,就使用 root_url。
修改 config/routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
end
如此一來,像是 Help 頁面,會把 /help 的 get 請求傳給 StaticPages controller 的 help action 處理,所以 /static_pages/help 就可以簡化成 /help。