Friday, October 11, 2013

Users Resource

Back in Griffith Park with Lars. It's a beautiful day, the kids are playing and the crows are making their hollow clicking noises that sound like woodpeckers.

I am working my way through Chapter 2

A break down of 2.2
  1. The browser issues a request for the /users URL.
    a. We start with a request issued from the browser
    i.e., the result of typing a URL in the address bar or clicking on a link.
  2. Rails routes /users to the index action in the Users controller.
    a. This request hits the Rails router which dispatches to the proper controller action based on the URL and the type of request. The code below creates the mapping of user URLs to controller actions for the Users resource. This code effectively sets up the table of URL/action pairs seen in Table 2.1

    config/routes.rb
    DemoApp::Application.routes.draw do
      resources :users
      .
      .
      .
    end
    
  3. The index action asks the User model to retrieve all users (User.all).
    a. This index action has the line @users = User.all

  4. app/controllers/users_controller.rb
    class UsersController < ApplicationController
    
      def index
        @users= User.all
      end
      .
      .
      .
    end
    
  5. The User model pulls all the users from the database.
    a.  @users = User.all  asks the User model to retrieve a list of all the users from the database
  6. The User model returns the list of users to the controller.
    a. Then it places all the users from the database in the variable @users

    app/models/user.rb
  7. class User < ActiveRecord::Base
    end
    
  8. The controller captures the users in the @users variable, which is passed to the index view.
    a. Once the @users variable is defined, the controller calls the view. Instance variables, variables that start with the @ sign, are automatically availablein the view; in this case, the index.html.erb view iterates through the @users list and outputs a line of HTML for each one.

    app/views/users/index.html.erb
  9. The view uses embedded Ruby to render the page as HTML.
    a. The view converts its contents to HTML

    app/views/users/index.html.erb (The view for the user index.) 
  10. <h1>Listing users</h1>
    
    <table>
      <tr>
        <th>Name</th>
        <th>Email</th>
        <th></th>
        <th></th>
        <th></th>
      </tr>
    
    <% @users.each do |user| %>
      <tr>
        <td><%= user.name%></td>
        <td><%=user.email %></td> 
            <td><%=link_to 'Show',user %></td>
            <td><%= link_to 'Edit',edit_user_path(user) %></td>
            <td><%= link_to 'Destroy',user, method  :delete,data:                    { confirm: 'Are you sure?' } %></td> </tr> 
    <% end %> </table> 
    <br /> 
    <%= link_to 'New User', new_user_path %>

  • The controller passes the HTML back to the browser.
    a. The HTML content is then returned by the controller to the browser for display

  • Leaving the park because a modeling shoot just moved in under the gazebo.

    REST Architecture (REpresentational State Transfer) REST is an architectural style for developing distributed, networked systems and software applications such as the World Wide Web and web applications. Although REST theory is rather abstract, in the context of Rails applications REST means that most application components (such as users and microposts) are modeled as that can be created, read, updated, and deleted—operations that correspond both to the HTTP request methods POST GET PATCH and DELETE.

    No comments:

    Post a Comment