I have a pages controller in which I support 3 static pages (for example : aboutus, faq and home). I don’t have any actions inside the controller, just the appropriate views so adding new pages will be easy.
I used the default route :
match ‘:controller(/:action(/:id))(.:format)’
and can now access my pages through : ‘http://localhost:3000/pages/faq’ (or any other page)
I’d like to give those routes names so when I create links to them from other pages I’ll use some thing like:
link_to 'FAQ' faq_path'
rather then
link_to ‘FAQ’ ‘/pages/faq’
Is that possible to create “dynamic named routes”? I’ve tried things like:
match 'pages/:action' => 'pages#:action', :as => :action
but it didn’t really work…
You can’t easily create dynamic methods like that, but if you think of it a different way you’re actually parameterising your path helper. Rails does have an easy way to do this: