Hemanth
Hemanth

Reputation: 5145

How the customize or change the route paths in rails?

For example consider the following output from rake routes:-
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
posts POST /posts(.:format) {:action=>"create", :controller=>"posts"}
*new_post* GET /posts/new(.:format) {:action=>"new", :controller=>"posts"}
*edit_post* GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}

If i want to change the path name of "new_post" to "create_post" how do I do it ??

Upvotes: 4

Views: 7789

Answers (4)

Swaps
Swaps

Reputation: 1548

In rails 5 you can do it like -

get '/create_post', to: 'posts#new'

You should prefer http method keywords instead of directly using match as per API docs. Also I would choose default RESTful routes generated by resources method in Rails instead defining all custom REST routes.

Upvotes: 1

bmac151
bmac151

Reputation: 525

A 'match' here is not appropriate. It can pose a security issue because by default it opens up the route you are matching to every request verb (get, post, etc). You should either specify the verb in place of match

get '/posts/new' => 'posts#new', as: :create_post

or you can specify what verbs you want to use, using match

match '/posts/new' => 'posts#new', as: :create_post, via: [:get]    

Upvotes: 3

Mohit Jain
Mohit Jain

Reputation: 43939

You must checkout railscast for routing in rails 3

Btwn solution for your problem

 match '/posts/new' => 'posts#new', :as => :create_post

Upvotes: 4

satya on rails
satya on rails

Reputation: 392

match '/posts/new' => 'posts#new', :as => :create_post in your routes should work!

Upvotes: 4

Related Questions