我有一个我创建的 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
无论我尝试什么,我都无法将页面路由到/name
so /about
or /contact
,而是收到一个错误消息:Couldn't find Cms::Page with id=about
or Couldn't find Cms::Page with id=contact
,但是如果我转到/1
or /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