Controllers

REST

Rails uses REST conventions to access resources. REST is a way of organizing HTTP requests. This is the mapping between a REST-style HTTP command, the corresponding Rails controller action, and the corresponding database command, as shown in Table B-2.

Table B-2. REST

 

Create

Read

Update

Delete

HTTP

POST

GET

PUT

DELETE

RAILS

CREATE

SHOW

UPDATE

DESTROY

Database

INSERT

SELECT

UPDATE

DELETE

These are the specific HTTP requests that will invoke these controller methods if the map resource :slide statement appears in routes.rb:

class SlidesController < ApplicationController
  # GET /slides
  # GET /slides.xml
  def index
    ...
  end

  # GET /slides/1
  # GET /slides/1.xml
  def show
    ...
  end

  # GET /slides/new
  # GET /slides/new.xml
  def new
    ...
  end

  # GET /slides/1/edit
  def edit
    ...
  end

  # POST /slides
  # POST /slides.xml
  def create
    ...
  end

  # PUT /slides/1
  # PUT /slides/1.xml
  def update
    ...
  end

  # DELETE /slides/1
  # DELETE /slides/1.xml
  def destroy
    ...
  end
end

Named routes

Mapping a resource in config/routes.rb will generate some named routes and some helpers:

map.resource :photo Named Route Helpers ============ ============================================= photo account_url, hash_for_account_url, account_path, hash_for_account_path new_photo new_account_url, hash_for_new_account_url, new_account_path, hash_for_new_account_path edit_photo edit_account_url, hash_for_edit_account_url, edit_account_path, hash_for_edit_account_path examples ======== link_to "Show a photo", photo link_to "Edit a photo", edit_photo_path(photo) ...

Get Rails: Up and Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.