It is a memorandum of Chapter 8.
Rails 6.0.3 Ruby 2.6.3
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
** 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.
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.
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.
application_controller.rb
(this time sessions_helper.rb
)app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include SessionsHelper
end
app/helpers/sessions_helper.rb
module SessionHelper
def log_in(user)
session[:user_id] = user.id
end
end
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.
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.
$ yarn add [email protected] [email protected]
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
app/javascript/packs/application.js
.
.
require("jquery")
import "bootstrap"
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_userand
logged_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