Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Route to Create Pages1:18 with Jay McGavren
When we submit our form for a new Page, we see the error "No route matches [POST] '/pages'". Notice that it's not saying [GET] '/pages', it's saying POST. We only have GET request routes for pages right now, though. We'll need to add a POST route.
When you're adding new data to the server, your browser sends an HTTP POST request. So, when you click the submit button on an HTML form, that's what your browser sends to the server: a POST request with the form contents.
For this reason, setting up a form to add new instances of a resource usually requires two routes in your
Suppose we were setting up a form for new
Page records. We'd first need a route to GET the HTML form:
get '/pages/new', to: 'pages#new'
Then we'd need a second route to accept the POST request when the form is submitted:
post '/pages', to: 'pages#create'
When we submit our form for a new page we see the error,
no route matches post /pages.
Notice that it's not saying get pages, tt's saying post.
HTTP Get requests are intended for getting data from the server.
When you're adding new data and
you're supposed to use an HTTP POST request instead.
So when you click the submit button on an HTML form
a POST request as what your browser sense to the server.
We only have get requests routes for
pages right now though, we'll need to add a post route.
We do that by calling the post method instead of the get method within route
that are RB.
We'll route all post requests for
the /pages path to the pages controllers create method.
We already have a get /pages route but
it's okay to have a post /pages route as well.
The two different request types get routed separately.
There's also no risk of the router confusing a post request with the other
get request, so we don't have to worry about what order we add the route in.
If we save this and submit our form again from our browser,
we'll see the action create cannot be found for PagesController.
It looks like our request is being routed to the right place.
Now we just need to add the create method to handle it.
You need to sign up for Treehouse in order to download course files.Sign up