【发布时间】:2013-11-03 06:18:33
【问题描述】:
我创建了一个 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 所以/about 或/contact,相反我收到一个错误消息:Couldn't find Cms::Page with id=about 或Couldn't find Cms::Page with id=contact,但如果我转到/1或/2,然后页面被渲染。
我的 rake 路线是:
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
【问题讨论】:
-
您是否检查过
params[:id]是什么,并手动尝试过Page#find_by_name? -
你能把
rake routes的结果贴出来吗?您想要实现的目标不是很清楚,您也不是现有的 cms 路线。 -
标签: ruby-on-rails ruby content-management-system routes