- Intro to Fast Rails Development
- Rails Quick Start
- Rails Scaffold
- Scaffold Tour I - Resources
- Scaffold II - Forms
- User Post Relationship
- Validations & Filters
- Correct User
- Adjusting the Pages
- Header Adjustments
- Simple Twist
- 1000 Words
- Live Site on Heroku
Build a quick blog platform with Rails. (Beta tutorial.)
Scaffold Tour I - Resources
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.
The scaffold added the following line to
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|
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.
resources keyword lets you quickly adds routes for standard CRUD operations in Rails. It creates seven routes in total for seven different actions:
- new - renders page with a form for creating new items.
- create - the new form gets submitted here as a POST request.
- show - renders page for showing one item.
- index - renders page for showing all items.
- 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.
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.
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.
show doesn't need any code in its body, since
@post is assigned by
show view page will be rendered and can display the contents of