How to change a route name rails 4
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! :)
Related videos on Youtube
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, 2022Comments
-
Kevin Dark almost 2 years
I changed the routing of
posts#index
to matchblog
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 useblog
rather thanposts
in the URL.
Below is my current route file. I am using thefriendly_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 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 about 10 yearsI 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 about 10 yearsFair enough. Thank you for the answer below.
-
-
Richard Peck about 10 yearsYou'll also want
as
if you want to rename your path helpers toblog
also -
FloatingRock over 9 years@kiddorails The only question now is how to get the views to load up from
views/blogs
instead ofviews/posts
?