22

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 
3
  • @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 6, 2014 at 7: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 6, 2014 at 7:31
  • Fair enough. Thank you for the answer below.
    – Kevin Dark
    Apr 6, 2014 at 7:32

3 Answers 3

44

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! :)

2
  • 2
    You'll also want as if you want to rename your path helpers to blog also Apr 6, 2014 at 7:23
  • @kiddorails The only question now is how to get the views to load up from views/blogs instead of views/posts? Oct 27, 2014 at 5:54
1

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

get '/patients/:id', to: 'patients#show', as: 'patient'
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.