0

这是我当前的 routes.rb 代码:

Mysite::Application.routes.draw do
  get "pages/home"

  get "pages/about"

  get "pages/resume"

  get "pages/contact"

  root :to => "pages#home"

  match "/about", to: "pages#about"
  match "/resume", to: "pages#resume"
  match "/contact", to: "pages#contact"
end

有没有办法简化根目录到页面控制器的路由?我可以将整个根目录与页面控制器本身匹配,而不是将根目录的每个路由都匹配到页面控制器操作吗?

4

4 回答 4

1

删除前四个gets。您的rootmatch声明将正常工作。

Mysite::Application.routes.draw do

  match "/about", to: "pages#about"
  match "/resume", to: "pages#resume"
  match "/contact", to: "pages#contact"
  root :to => "pages#home"

end
于 2013-05-29T00:08:01.797 回答
0

如果你真的想将整个根目录路由到 pages 控制器,你可以画一个catath all路由:

  match '/*path', to: 'pages#show'

在您的路线结束时这样做会捕获所有先前未定义的请求。在您的 pages#show 中,您可以检查路径并尝试找到匹配的页面,即

  search =  params[:path].split('/').last

获取路径的最后一部分并且

  @page = Page.find_by_titel_or_id search

尝试按标题查找匹配的页面。(这样由 CMS 完成,如炼油厂_cms)

于 2013-05-29T07:15:19.513 回答
0

如果你想要像“pages/about”这样的路径,那么

Mysite::Application.routes.draw do

  root :to => "pages#home"

  scope '/pages' do
    match "/about", to: "pages#about"
    match "/resume", to: "pages#resume"
    match "/contact", to: "pages#contact"
  end

end
于 2013-05-29T06:44:42.577 回答
0
Mysite::Application.routes.draw do
# Layout
  resources :pages do
    get 'about' => 'pages#about', :on => :collection
    get 'resume' => 'pages#resume', :on => :collection
    get 'contact' => 'pages#contact', :on => :collection
  end
  match 'About' => 'pages#about', :as => :About
  match 'Resume' => 'pages#resume', :as => :Resume
  match 'Contact' => 'pages#contact', :as => :Contact
  root :to => "pages#home"
end

现在您可以直接使用此匹配项,例如

<a href='/Contact'>Contact Us</a>
于 2013-05-29T07:21:16.323 回答