Rails 4 match all routes for one controller

我们两清 提交于 2019-12-12 05:12:30

问题


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'

回答1:


Why don't you just use the show 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

This way, you can pass the "page" directly through the link and have it all handled by Rails:

 <%= link_to "About", static_path("page") %>



回答2:


You probably need something like get 'static/:action', to: 'static#show' and then in your StaticController show action render the correct static page based on the params[:action] parameter.

See http://guides.rubyonrails.org/routing.html#defining-defaults for more.




回答3:


You can route something like

get '*path', to: 'static#show'


来源:https://stackoverflow.com/questions/35024923/rails-4-match-all-routes-for-one-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!