Demo of controllers and resources

Setup

  • We’re going to build a simple blogging system
  • Posts and comments
  • Do these commands in the shell
rails new resdemo
rails generate scaffold Post title:string body:text
rails generate scaffold Comment body:text post_id:integer
rake db:migrate

Review

  • Examine db/migrations/*
  • Examine app/models/*
  • Examine config/routes.rb
  • Examine app/controllers
  • Examine app/views

Routes

  • Run rake routes
      Prefix Verb   URI Pattern                  Controller#Action
    comments GET    /comments(.:format)          comments#index
             POST   /comments(.:format)          comments#create
 new_comment GET    /comments/new(.:format)      comments#new
edit_comment GET    /comments/:id/edit(.:format) comments#edit
     comment GET    /comments/:id(.:format)      comments#show
             PATCH  /comments/:id(.:format)      comments#update
             PUT    /comments/:id(.:format)      comments#update
             DELETE /comments/:id(.:format)      comments#destroy
       posts GET    /posts(.:format)             posts#index
             POST   /posts(.:format)             posts#create
    new_post GET    /posts/new(.:format)         posts#new
   edit_post GET    /posts/:id/edit(.:format)    posts#edit
        post GET    /posts/:id(.:format)         posts#show
             PATCH  /posts/:id(.:format)         posts#update
             PUT    /posts/:id(.:format)         posts#update
             DELETE /posts/:id(.:format)         posts#destroy

Play with the server

  • Run the rails server (rails server)
  • Access these urls from your browser observe error messages, and refer to rake routes above
    • http://0.0.0.0:3000/posts
    • http://0.0.0.0:3000/posts/1
    • http://0.0.0.0:3000/comments/1
    • http://0.0.0.0:3000/posts/1/comments/1
  • Note: no nesting!

Update the controllers

  • Now we add some code for controllers and views
rails generate controller Posts new create update edit destroy index show
rails generate controller Comments new create update edit destroy index show
  • Experiment with the urls again
    • http://0.0.0.0:3000/posts/1/comments/1
    • Nesting still doesn’t work

Nesting happens in routing

  • Before:
1# config/routes.rb
2  resources :posts
3  resources :comments
  • After:
1# config/routes.rb
2  resources :posts do
3    resources :comments
4  end

Review results

  • Review rake routes
  • Illustrate what is happening by adding params.inspect in various views.