Zach
Zach

Reputation: 880

Ruby on Rails - How do I use dashes instead of underscores in the url?

When I ran the rails generate controller Pages index sample_page it created my two controllers. I can see the page at localhost:3000/sample_page, but how do I change it to show dashes instead of underscores in the url ie: localhost:3000/sample-page

Rails doesn't like it when you create a controller with dashes, so I have to use underscore. What do I need to add to my routes.rb file?

Thanks!

Upvotes: 1

Views: 2753

Answers (2)

jbmyid
jbmyid

Reputation: 2015

use path in routing

resources :pages do
  collection do
    get :contact_us, path: "contact-us"
  end
end

Upvotes: 5

Teej
Teej

Reputation: 12883

Note that you should probably just create one controller for your pages.

To answer your question, you can do some custom routes eg:

  match '/about-us' => 'pages#about'
  match '/contact-us' => 'pages#contact'
  match '/terms-and-conditions' => 'pages#terms'

Upvotes: 2

Related Questions