How to change a route name rails 4

21,319

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

Share:
21,319

Related videos on Youtube

Kevin Dark
Author by

Kevin Dark

Ten years ago while building my career in the financial services industry, I discovered application development and the product management discipline which changed my entire career path and goals. I've been fortunate to work on products in both tactical and strategic capacities within the financial services and telecommunications industry. I enjoy learning and growing my professional skills. At the moment I have been growing my functional programming knowledge work with Elixir and Phoenix.

Updated on July 09, 2022

Comments

  • Kevin Dark
    Kevin Dark almost 2 years

    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 
    
    • Kevin Dark
      Kevin Dark about 10 years
      @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?
    • kiddorails
      kiddorails about 10 years
      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 :)
    • Kevin Dark
      Kevin Dark about 10 years
      Fair enough. Thank you for the answer below.
  • Richard Peck
    Richard Peck about 10 years
    You'll also want as if you want to rename your path helpers to blog also
  • FloatingRock
    FloatingRock over 9 years
    @kiddorails The only question now is how to get the views to load up from views/blogs instead of views/posts?