An Update Action5:52 with Jay McGavren
We've successfully created a form for editing an existing Page, and populated it with the Page's attributes. But if we submit the form, we see that a so-called HTTP PATCH request is being sent by the browser, and there's no route for that type of request. So to process these requests, we'll need to go into routes.rb add a PATCH route.
A controller action to update an existing model object usually performs these operations:
def update # Look up the existing model record based # on an ID from the request path. @page = Page.find(params[:id]) # Filter the form parameters to ensure no # malicious parameters were added. page_params = params.require(:page).permit(:title, :body, :slug) # Use the filtered parameters to update # the existing model record. @page.update(page_parameters) # Redirect the browser to another location # so that it doesn't just sit there displaying # the submitted form. redirect_to @page end
You need to sign up for Treehouse in order to download course files.Sign up