Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Rails Routes and Resources Routes to Create Actions Route to Create Pages

Why post 'pages',to 'pages#create' and why not post 'pages/new',to 'pages#create'

Why post 'pages',to 'pages#create' and why not post 'pages/new',to 'pages#create'

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya there! Rails is a very opinionated MVC Framework and it follows CoC (convention over configuration) and DRY very well. As in every MVC framework, Routes pretty much routes the request to the controller. And the controller in turn read the request and gets the appropriate information from the model and sends it to the view to render. To check all the routes , you can just simply type rails routes in your terminal.

Now, In Rails, a single controller class might contain eight actions(as per convention): new , show, create, index, edit, update, destroy, delete. And these actions have their own views and routes. So every request for index gets routed to "pages#index" ( which most of the times is the root route). One thing to notice here is that its a GET request , right? Similarly, request pages#new is a GET request and will be available as new_pages_path, which will route the user to a some kinda form, right? So when the current_user will fill out this form, he'll get routed to pages#create controller 'coz create action processes the user's request , which will be POST request. And is available as /pages or pages_path just like our index action.

I hope it helped!

~ Ari

so you are telling /pages used in the POST command is used to redirect the page to the index page If so then why do we use redirect if this can redirect on its own

Ari Misha
Ari Misha
19,323 Points

kavan Sharma Nahh the request for create action is a POST request whereas the request for index action is a GET request. But the both will have url as /pages. See the difference? For further reading , you can visit here.