Rails 4匹配一个控制器的所有路线
问题描述:
我有一个带有多个静态页面的控制器,并且理想情况下,我希望使用通配符来路由所有这些页面。
I have a controller with a number of static pages and I would ideally like to route them all with a wildcard.
是否可以做一些喜欢以下内容?
Is it possible to do something like the following?
get 'static/:action'
答
为什么不只使用显示
操作:
#config/routes.rb
resources :static, param: :page, only: :show #-> url.com/static/:page
#app/controllers/static_controller.rb
class StaticController < ApplicationController
def show
render "#{params[:page]}"
end
end
这样,您可以直接通过链接传递页面,并由Rails处理:
This way, you can pass the "page" directly through the link and have it all handled by Rails:
<%= link_to "About", static_path("page") %>