20

I changed the routing of posts#index to match blog and I now get /blog in the URL which I was trying to accomplish.

I've tried several different things to get my actual blog post which the route currently looks something like /posts/this-is-a-test to also use blog rather than posts in the URL.
Below is my current route file. I am using the friendly_id gem, if that makes any difference in answering this question.

resources :posts do
  resources :comments
end

  resources :contacts, only: [:new, :create]

  root "pages#home"

  get "/home", to: "pages#home", as: "home"
  get "about" => 'pages#about'
  get "pricing" => 'pages#pricing'
  get "contact_us" => 'pages#contact_us'
  match 'blog', to: 'posts#index', via: :all
end 
kiddorails
  • 12,474
  • 2
  • 29
  • 39
Kevin Dark
  • 1,969
  • 5
  • 20
  • 23
  • @kiddorails thank you for cleaning up my question. I'm just starting to use stack more frequently and getting familiar with all the formatting. Reviewing your edits has been helpful in seeing where I can improve. Out of curiosity, why would you edit out a Thank you at the end of my question to show appreciation to folks in the community? – Kevin Dark Apr 06 '14 at 07:28
  • I am glad that it is helping you to grasp the question formatting etc. Personally, I wish to directly look and understand the question requirements; your 'thank-you' note was bigger than a line and was repeating the context of the question, so I removed it :). You could always attach just 'Thanks' or 'Thank you!' in your question though :) – kiddorails Apr 06 '14 at 07:31
  • Fair enough. Thank you for the answer below. – Kevin Dark Apr 06 '14 at 07:32

3 Answers3

39

path option along with resource must help.

resources :posts, :path => 'blogs' do
  resources :comments
end

This will change all /posts and /post to /blogs/ and /blog.

If you want to change your route's helper methods such as posts_path to blogs_path and new_post_path to new_blog_path etc, you can change it with as tag.

resources :posts, :path => 'blogs', :as => 'blogs' do
  resources :comments 
end

Or yet better, you can specify the controller and route blogs directly as:

resources :blogs, controller: 'posts' do
  resources :comments
end

This is the awesomeness of Rails! :)

kiddorails
  • 12,474
  • 2
  • 29
  • 39
0
match 'blog/:id' => 'posts#show'

should work. But if you want to match every method in posts controller to blog (and you don't want to use the posts path), I would just rename the controller to blog instead and add resource :blog in the routes.

Giannis
  • 793
  • 5
  • 13
0

This helped me, from official guides documentation, add this in your routes.rb file:

get '/patients/:id', to: 'patients#show', as: 'patient'
facureyes
  • 21
  • 2
  • 4