Rails Tutorial Chapter 8 Notes

It is a memorandum of Chapter 8.

environment

Rails 6.0.3 Ruby 2.6.3

table of contents

1 session and cookies 2 Flash message display 3 Flow of creating and using helper method 4 How to install jquery and bootstrap 5 Do not log in again after signing up

1 session and cookies

** HTTP **, which is used for daily communication, is a ** stateless protocol **. What is stateless? Please display the page of XX on the browser side (request) → The page of XX is on the server side! The exchange of (response) is called a transaction, but once the exchange is completed, it is completely forgotten.

In other words, if you log in on the member site and then send a request to visit the purchase page for this product, you've forgotten your previous login interaction, so ** Who are you? It becomes **.

To prevent this, the session is set to hold the user's ID.

And it is ** cookies ** that are used to implement that session. Cookies are small, kist data that can be stored in your browser. The login function is created by storing the user's ID when logging in here and deleting the one stored in cookies when logging out.

2 Flash message display

The flash message implemented in Chapter 7 that is displayed at the time of new registration was associated with the ActiveRecord object, but since ActiveRecord is not used at the time of the session, it needs to be created by another method.

app/controllers/sessions_controller.rb


class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      #Redirect to user information page after user login
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end
  .
  .
end

As an implementation flow, Use the flash method when you want to display it. Since it is laid out including CSS in application.html.erb, it is automatically displayed in this one line.

Also, since the flash method alone causes a small bug that does not disappear even if it is re-rendered, use the flash.now method that includes the function to turn off the display when a request occurs after displaying the flash message.

3 Flow of creating and using helper methods

At this point, conditional branching occurs frequently depending on whether or not you are logged in for each Controller. If you concisely summarize the code each time as a method, the code will be simplified, so the flow of creating and using the method yourself will be summarized.

  1. Load the helper module into application_controller.rb (this time sessions_helper.rb)

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  include SessionsHelper
end
  1. Create your own method in the helper file (this time log_in method)

app/helpers/sessions_helper.rb


module SessionHelper
  def log_in(user)
    session[:user_id] = user.id
  end
end
  1. Used in Controller file

appp/controllers/sessions_controller.rb


.
.
  def create
  .
  .
  log_in user
  .
  end
.

By doing this, you can actually use the method. This time, the content of the log_in method is also one line, but if you assume that you will create a method with multiple lines in the future and repeat the same operation, by doing this, you can improve readability by setting the method name. It leads to being DRY.

4 How to install jquery and bootstrap

Has the setting method changed from Rails6? It seems that it may be, so this time I will summarize the setting method in Rails 6.

  1. Install with yarn
$ yarn add [email protected] [email protected]
  1. Add jQuery settings to Webpack

config/webpack/environment.js


const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

module.exports = environment
  1. Requires / import required JS files

app/javascript/packs/application.js


.
.
require("jquery")
import "bootstrap"

5 Do not log in again after signing up

Is it easy to forget if you create a login function after creating a signup function? However, when I actually sign up, I am not logged in.

The actual movement is that the ** show ** view of / users/◯ is displayed with redirect_to @user, but since the user ID is not saved in the session, the created using the session The current_userandlogged_in` methods cannot be used, or false is returned.

To prevent this from happening, don't forget log_in @ user when the user is saved with the create action during signup.

It may not be the time to write it, but if you find yourself annoyed with user registration, if you have to re-enter it on the login screen after new registration, gently close the app. I think. .. Lol

A memo for a little usability.

Recommended Posts

Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 8 Notes
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
Rails Tutorial Chapter 3 Learning
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
rails tutorial fighting notes Ⅲ
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
rails tutorial
rails tutorial
rails tutorial
rails tutorial
[Rails Tutorial Chapter 4] Rails-flavored Ruby
rails tutorial
rails tutorial
rails tutorial
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Chewing Rails Tutorial [Chapter 2 Toy Application]
Rails Tutorial (4th Edition) Memo Chapter 6
Rails tutorial test
Rails tutorial memorandum 1
Rails tutorial memorandum 2
Start Rails Tutorial
[Beginner] Rails Tutorial
html & rails notes
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Chapter 4 Rails Flavored Ruby
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
Rails Tutorial cheat sheet
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
[Rails] Learning with Rails tutorial
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Rails Tutorial 4th Edition: Chapter 1 From Zero to Deployment
Resolve ActiveRecord :: NoDatabaseError when doing rails test (Rails tutorial Chapter 3)
Ruby on Rails Tutorial Troublesome notes when running on Windows
11.1 AccountActivations Resources: Rails Tutorial Notes-Chapter 11
Rails Tutorial Records and Memorandum # 0
Rails Tutorial (4th Edition) Summary
[Rails] New app creation --Notes--
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] Second Half
Chewing Rails Tutorial [Chapter 1 From Zero to Deployment] First Half
Rails Tutorial Chapter 14 Creating Relationship Test Data with Factory Bot