若是是使用Rails的默認約定,那麼幾乎是零配置。
但有些時候,咱們可能不得不(或者更喜歡)進行一些特殊的配置。spa
其實Rails在路由功能中也有很豐富的配置選項。code
routes.rb
文件中靠前的規則優先級更高。能夠手工設定路由:orm
get 'meetings/:id' => 'events#show'
典型路由,匹配URL:資源
match ':controller(/:action(/:id(.:format)))', :via => :all
括號表示能夠省略。等價於下面這六個規則:路由
match '/:controller', via: :all match '/:controller/:action', via: :all match '/:controller/:action/:id', via: :all match '/:controller.:format', via: :all match '/:controller/:action.:format', via: :all match '/:controller/:action/:id.:format', via: :all
能夠對匹配作一些限定。如限定整數id:get
match "/events/show/:id" => "events#show", :constraints => {:id => /\d/}
路由規則能夠命名爲一個helper,如:io
get '/meetings' => 'events#index', :as => "meetings"
能夠產生meetings_path,對應'/meetings' => 'events#index'這種路由。event
重定向:form
get "/foo" => redirect("/bar")
設置首頁:配置
root :to => 'welcome#show'
嵌套資源的路徑:
resources :projects do resources :tasks end
這樣產生的Helper爲project_tasks_path(@project)
和project_task_path(@project, @task)
,網址如projects/123/tasks
和projects/123/tasks/123
。
其餘的配置如scope
和namespace
暫時用不到,之後用到了再去看。