Scaffold Tour I - Resources


Collapse Content

The scaffold's code let's you can see the standard "Rails" way to set up your app. Let's explore the code to see how everything works.

Restful Resources

The scaffold added the following line to routes.rb:

resources :posts

As mentioned earlier, you can navigate to /rails/info/routes to view all the routes in your app. This lets you view all the resource routes that were just created:

Helper Path HTTP Verb   Path Controller#Action
posts_path POST /posts posts#create
new_post_path GET /posts/new posts#new
edit_post_path GET /posts/:id/edit posts#edit
post_path GET /posts/:id posts#show
PATCH /posts/:id posts#update
DELETE /posts/:id posts#destroy
GET /posts posts#index

As discussed in Rails Links, the left column shows the named helper Rails provides to make it easier to create links. Each helper has a specific HTTP verb or method associated with it. HTTP methods are defined by the HTTP protocol to indicate what kind of action should happen for a specific web request.

GET is used to request a specific web page without modifying anything. Whenever you enter a URL in your browser, it submits a GET request. In Rails, GET is used to display items in :show and :index and to display form pages for :edit, and :new. You can use a standard link_to helper to link to these pages.

A different HTTP request is used to modify items:

  • POST - To create a new item
  • PATCH (or PUT) - To update an item
  • DELETE - To delete an item

When a form is submitted, it sends a POST or PATCH request to the specified route, which is then routed to the relevant controller action. As you'll see in the next page, Rails helps you set up these forms so they pass the form information to correct paths.

CRUD Controllers

The resources keyword lets you quickly adds routes for standard CRUD operations in Rails. It creates seven routes in total for seven different actions:

Create

  • new - renders page with a form for creating new items.
  • create - the new form gets submitted here as a POST request.

Read

  • show - renders page for showing one item.
  • index - renders page for showing all items.

Update

  • edit - renders page with a form for editing existing items
  • update - the edit form gets submitted here as a PATCH request (or a PUT request.).

Delete - submit request here to destroy an item.

Controllers

The routes are set up to go to the appropriate controller actions, which should then perform the expected actions. The scaffold adds the seven actions to the controller along with the default code to perform each action.

Here's the index action:

  def index
    @posts = Post.all
  end

This assigns the @posts variable to all posts, so they can be displayed in the view. If you only wanted to display certain posts (like in Presenting Products) you could customize it here.

Now let's look at the show action, along with the beginning and end of the controller:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def show
  end

 private
   # Use callbacks to share common setup or constraints between actions.
   def set_post
     @post = Post.find(params[:id])
   end

#...
end

before_action is used to run code before a controller action. It can be used as part of Authorization to control access to certain action, but it can also be used to share code between methods. It's used above to assign a @post variable for relevant actions. The method set_post uses the id in the URL to get the post with that ID.

The method show doesn't need any code in its body, since @post is assigned by set_post. The show view page will be rendered and can display the contents of @post.

Contact Us
Sign in or email us at [email protected]