Agile Web Development with Rails 5 (for Sandra Beaugh) by Sam Ruby Dave Thomas David Heinemeier Hansson
Author:Sam Ruby, Dave Thomas, David Heinemeier Hansson
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: The Pragmatic Bookshelf, LLC (598534)
If the user successfully logs in, we store the ID of the user record in the session data. We’ll use the presence of that value in the session as a flag to indicate that an administrative user is logged in.
As you might expect, the controller actions for logging out are much simpler:
rails50/depot_r/app/controllers/sessions_controller.rb
def destroy
» session[:user_id] = nil
» redirect_to store_index_url, notice: "Logged out"
end
Finally, it’s about time to add the index page—the first screen that administrators see when they log in. Let’s make it useful. We’ll have it display the total number of orders in our store. Create the template in the index.html.erb file in the app/views/admin directory. (This template uses the pluralize helper, which in this case generates the order or orders string, depending on the cardinality of its first parameter.)
rails50/depot_r/app/views/admin/index.html.erb
<h1>Welcome</h1>
It's <%= Time.now %>
We have <%= pluralize(@total_orders, "order") %>.
The index action sets up the count:
rails50/depot_r/app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
» @total_orders = Order.count
end
end
We have one more task to do before we can use this. Whereas previously we relied on the scaffolding generator to create our model and routes for us, this time we simply generated a controller because there’s no database-backed model for this controller. Unfortunately, without the scaffolding conventions to guide it, Rails has no way of knowing which actions are to respond to GET requests, which are to respond to POST requests, and so on, for this controller. We need to provide this information by editing our config/routes.rb file:
rails50/depot_r/config/routes.rb
Rails.application.routes.draw do
» get 'admin' => 'admin#index'
» controller :sessions do
» get 'login' => :new
» post 'login' => :create
» delete 'logout' => :destroy
» end
resources :users
resources :orders
resources :line_items
resources :carts
root 'store#index', as: 'store_index'
resources :products do
get :who_bought, on: :member
end
# For details on the DSL available within this file, see
# http://guides.rubyonrails.org/routing.html
end
We’ve touched this before, when we added a root statement in Iteration C1: Creating the Catalog Listing. What the generate command will add to this file are fairly generic get statements for each of the actions specified. You can (and should) delete the routes provided for sessions/new, sessions/create, and sessions/destroy.
In the case of admin, we’ll shorten the URL that the user has to enter (by removing the /index part) and map it to the full action. In the case of session actions, we’ll completely change the URL (replacing things like session/create with simply login) as well as tailor the HTTP action that we’ll match. Note that login is mapped to both the new and create actions, the difference being whether the request was an HTTP GET or HTTP POST.
We also make use of a shortcut: wrapping the session route declarations in a block and passing it to a controller class method. This saves us a bit of typing as well as makes the routes easier to read. We’ll describe all you can do in this file in Dispatching Requests to Controllers.
With these routes in place, we can experience the joy of logging in as an administrator. See the following screenshot.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6822)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6553)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6419)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3994)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3785)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3727)
The Ultimate iOS Interview Playbook by Avi Tsadok(3703)
