Reputation: 1087
Here's my current routes.rb code:
Mysite::Application.routes.draw do
get "pages/home"
get "pages/about"
get "pages/resume"
get "pages/contact"
root :to => "pages#home"
match "/about", to: "pages#about"
match "/resume", to: "pages#resume"
match "/contact", to: "pages#contact"
end
Is there a way to simplify my routing of the root directory to the pages controller? Instead of matching every single route off the root directory to a pages controller action, could I instead match the entire root directory to the pages controller itself?
Upvotes: 0
Views: 174
Reputation: 6542
Mysite::Application.routes.draw do
# Layout
resources :pages do
get 'about' => 'pages#about', :on => :collection
get 'resume' => 'pages#resume', :on => :collection
get 'contact' => 'pages#contact', :on => :collection
end
match 'About' => 'pages#about', :as => :About
match 'Resume' => 'pages#resume', :as => :Resume
match 'Contact' => 'pages#contact', :as => :Contact
root :to => "pages#home"
end
Now you can use directly this matches like
<a href='/Contact'>Contact Us</a>
Upvotes: 0
Reputation: 8638
If you really want to route the entire root directory to the pages controller, you can draw a cath all route:
match '/*path', to: 'pages#show'
doing so at the end of your routes catches all requests not previeously defined. In your pages#show you can inspect the path and try to find matching pages i.e.
search = params[:path].split('/').last
gets the last part of the path and
@page = Page.find_by_titel_or_id search
tries to find an matching page by title. (So done by CMS like refinery_cms)
Upvotes: 0
Reputation: 1
If you want that path like "pages/about" then
Mysite::Application.routes.draw do
root :to => "pages#home"
scope '/pages' do
match "/about", to: "pages#about"
match "/resume", to: "pages#resume"
match "/contact", to: "pages#contact"
end
end
Upvotes: 0
Reputation: 6682
Remove the first four get
s. Your root
and match
declarations will work just fine.
Mysite::Application.routes.draw do
match "/about", to: "pages#about"
match "/resume", to: "pages#resume"
match "/contact", to: "pages#contact"
root :to => "pages#home"
end
Upvotes: 1