Chapter 8 - nortonbino/sample_app GitHub Wiki

Cap 8

8.1.1

Ex 1) The Get is to render the login page and the Post is to create the session without giving information in the url.

Ex 2)

norton@norton:~/workspace/sample_app$ rake routes | grep user
   signup GET    /signup(.:format)         users#new
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy

8.1.2

Ex 1) The submission call the post method from the "/login" action in the html.

8.1.3

Ex 1)

irb(main):006:0> user && user.authenticate(nil && [anything])
=> false
irb(main):007:0> user && user.authenticate(user && nil)
=> false
irb(main):008:0> user && user.authenticate(user && "1235678765432")
=> false
irb(main):009:0> user && user.authenticate(user && "123456")
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: "$2a$10$8yOu1mK6soMXttvOlA528eXzMBlDgwoZHHmbqP3o8yk...">

8.1.4

Ex 1) Yes, the sequence works and the flash message disappears when you click on a second page or when you refresh the page.

8.2.1

Ex 1) Name: _sample_app_session Value: ellRakVGODdpMEV6ZkgxelIvZ1Z2VWNiVk9MbWNjWHZNb2dlU3YwUSt5YkRaU3ZtY3h3UVczUGtqK1JqN1hLZGpzV1Y2eG9TdUQrV0t3aFp0eS9UK0gzQTNPYUNQYjBjU1VVb2JLSFRlTERBQlRtbDVMck9HVW9Bc2hHbW0yQ1V0NEQxTU9nOG9sQlBkbE1xbnFqZEQ3WVdHSk9IY3ppRUQ0UkwvdlR1ejhjPS0tOE5SWTZlUVlnSVU3VFdDRGdkYmM2UT09

Ex 2) No final da sessão

8.2.2

Ex 1)

irb(main):001:0> User.find_by(id: 100)
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  ["id", 100], ["LIMIT", 1](/nortonbino/sample_app/wiki/"id",-100],-["LIMIT",-1)
=> nil

Ex 2)

irb(main):002:0> session = {}
=> {}
irb(main):003:0> session[:user_id] = nil
=> nil
irb(main):004:0> @current_user ||= User.find_by(id: session[:user_id])
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ?  ["LIMIT", 1](/nortonbino/sample_app/wiki/"LIMIT",-1)
=> nil
irb(main):005:0> session[:user_id]= User.first.id
  User Load (0.7ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ?  ["LIMIT", 1](/nortonbino/sample_app/wiki/"LIMIT",-1)
=> 1
irb(main):006:0>  @current_user ||= User.find_by(id: session[:user_id])
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  ["id", 1], ["LIMIT", 1](/nortonbino/sample_app/wiki/"id",-1],-["LIMIT",-1)
=> #<User id: 1, name: "Norton", email: "[email protected]", created_at: "2017-09-26 15:44:00", updated_at: "2017-09-26 15:44:00", password_digest: "$2a$10$4tH22j8Mh8Gjm0GDjReZ8OMmEu0wKq4EMh36lIv3yD0...">
irb(main):007:0> @current_user ||= User.find_by(id: session[:user_id])
=> #<User id: 1, name: "Norton", email: "[email protected]", created_at: "2017-09-26 15:44:00", updated_at: "2017-09-26 15:44:00", password_digest: "$2a$10$4tH22j8Mh8Gjm0GDjReZ8OMmEu0wKq4EMh36lIv3yD0...">

8.2.3

Ex 1) Yes.

Ex 2) Yes.

8.2.5

Ex 1) Red.