有没有办法编写以下路由,这样您就不必每次都指定相同的控制器?...
get 'jobs' => 'pages#jobs'
get 'contact' => 'pages#contact'
get 'terms' => 'pages#terms'
get 'privacy' => 'pages#privacy'
这里有几个替代方案:
在这三者中,第一个,即Using scope as "/"
将创建与routes defined in the question
创建的路线完全相同的路线。
1. 使用范围作为"/"
scope "/", controller: :pages do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
创建路由,如下所示:
jobs GET /jobs(.:format) pages#jobs
contact GET /contact(.:format) pages#contact
terms GET /terms(.:format) pages#terms
privacy GET /privacy(.:format) pages#privacy
2. 使用范围作为"页面"
scope :pages, controller: :pages do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
创建路由,如下所示:
jobs GET /pages/jobs(.:format) pages#jobs
contact GET /pages/contact(.:format) pages#contact
terms GET /pages/terms(.:format) pages#terms
privacy GET /pages/privacy(.:format) pages#privacy
3. 筑巢路线
resources :pages do
member do
get 'jobs'
get 'contact'
get 'terms'
get 'privacy'
end
end
创建路由,如下所示:
jobs_page GET /pages/:id/jobs(.:format) pages#jobs
contact_page GET /pages/:id/contact(.:format) pages#contact
terms_page GET /pages/:id/terms(.:format) pages#terms
privacy_page GET /pages/:id/privacy(.:format) pages#privacy