0

我有一个我创建的 CMS 引擎,并将它链接到我的主站点,现在我试图让它路由到/about而不是/1,它映射到 CMS 引擎页面控制器显示操作。

example pages setup
id | name    | title      | body
1  | about   | About Us   | this is the about page
2  | contact | Contact Us | this is the contact page

它将成功路由到/1/2

这是我routes.rb的主应用程序,它加载 cms 引擎

  mount Cms::Engine, :at => '/cms', :as =>'cms'
  mount Blog::Engine, :at => '/blog', :as => 'blog'

  # route to cms pages
  match ":id", :to => 'cms/pages#show', :via => [:get, :post]

这是 CMS 页面控制器的显示操作

# GET /pages/1  or GET /pages/name
def show
  begin
     @page = Page.find_by_name(params[:id]) 
     @page ||= Page.find(params[:id]) 
  rescue
    redirect_to "/404.html"
  end
end

无论我尝试什么,我都无法将页面路由到/nameso /aboutor /contact,而是收到一个错误消息:Couldn't find Cms::Page with id=aboutor Couldn't find Cms::Page with id=contact,但是如果我转到/1or /2,则会呈现页面。

我的耙子路线是:

Prefix Verb     URI Pattern    Controller#Action
 cms          /cms           Cms::Engine
blog          /blog          Blog::Engine
     GET|POST /:id(.:format) cms/pages#show

Routes for Cms::Engine:
    pages GET    /pages(.:format)          cms/pages#index
          POST   /pages(.:format)          cms/pages#create
 new_page GET    /pages/new(.:format)      cms/pages#new
edit_page GET    /pages/:id/edit(.:format) cms/pages#edit
     page GET    /pages/:id(.:format)      cms/pages#show
          PATCH  /pages/:id(.:format)      cms/pages#update
          PUT    /pages/:id(.:format)      cms/pages#update
          DELETE /pages/:id(.:format)      cms/pages#destroy
     root GET    /                         cms/login#index

Routes for Blog::Engine:
root GET / blog/index#index
4

1 回答 1

1

好的,所以我想通了,基本上show默认使用 id,所以我简单地创建了一个显示操作,现在它可以工作了。

def display
  @page = Page.find_by_name(params[:id])
  render 'public/404.html', :status => 404 if @page.nil?
end
于 2013-11-03T19:33:21.167 回答