[RUBY] Implement login function simply with name and password in Rails (3)

Last time continued


Although the login function was available up to the last time, there remains a defect that it is not possible to know which user is logged in in the current state, and the list page can be accessed without logging in. Deal with it


--current_room method. Add a helper method to show the currently logged in user


  #If there is a logged-in user, substitute the user
  def current_room
    if session[:room_id]
      @current_room ||= Room.find_by(id: session[:room_id])

--This time, there is nothing in common on the pages, so it is unnecessary, but if necessary, define a helper method to determine whether you are logged in.


  #Determine if you are logged in
  def logged_in?

--Write the logout method in two places.


  def log_out
    @current_room = nil


  def destroy
    redirect_to login_path

--After that, add a link for displaying the logged-in user and logging out as shown below.

Login controlled page

 <div class="header-title">
    <div class="header-title">Reception list: 
    <%= current_room.name %>
    <%= link_to "Logout", logout_path, method: :delete %>

--Lastly, I want to control login before accessing the list screen, so define it in the private method.


class ReceptionsController < ApplicationController
  before_action :logged_in_room, only: [:index]
    def logged_in_room
      unless logged_in?
        flash[:alert] = "login is needed"
        redirect_to login_path

This completes the login function to the list page, although it is easy.

Recommended Posts

Implement login function simply with name and password in Rails (3)
Implement login function in Rails simply by name and password (1)
Implement login function in Rails simply by name and password (2)
Implement simple login function in Rails
Implement application function in Rails
Implement follow function in Rails
Login function implementation with rails
[Ruby on Rails] Implement login function by add_token_to_users with API
Implement CSV download function in Rails
Implement user registration function and corporate registration function separately in Rails devise
With Spring boot, password is hashed and member registration & Spring security is used to implement login function.
[Rails] Function restrictions in devise (login / logout)
Rails Addition of easy and easy login function
Try to implement login function with Spring-Boot
Make a login function with Rails anyway
[Ruby on Rails] How to log in with only your name and password using the gem devise
Implement star rating function using Raty in Rails6
Try to implement login function with Spring Boot
Implement the Like feature in Ajax with Rails.
[Rails] Implement search function
Implement markdown in Rails
How to implement guest login in 5 minutes in rails portfolio
Implement post search function in Rails application (where method)
[Rails] Implementation of drag and drop function (with effect)
Implement user follow function in Rails (I use Ajax) ①
Try to implement tagging function using rails and js
How to implement authentication process by specifying user name and password in Spring Boot
[Rails] Implement User search function
Introduced graph function with rails
Japaneseize using i18n with Rails
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
Implement import process in Rails
[Rails] Implement image posting function
[Rails] I tried to implement "Like function" using rails and js
Implement search function with form_with
[Rails] A simple way to implement a self-introduction function in your profile
Let's roughly implement the image preview function with Rails + refile + jQuery.
I tried to implement Ajax processing of like function in Rails
I tried to implement the image preview function with Rails / jQuery
Implement star evaluation function in Rails 6 (Webpacker is installed as standard)
Add a search function in Rails.
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[rails] Login screen implementation in devise
Implemented mail sending function with rails
Create pagination function with Rails Kaminari
Implement tagging function in form object
Implement PHP implode function in Java
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
How to implement search function with rails (multiple columns are also supported)
[Rails] How to log in with a name by adding a devise name column
Implement login, user registration, and two-factor authentication with Docker x Laravel 8 Jetstream
Sign in with Apple using gem's apple_id in Rails and cache Apple's JWK
[Rails 6] Register and log in with Devise + SNS authentication (multiple links allowed)
I wrote a Lambda function in Java and deployed it with SAM
[Rails] Implement the product purchase function with a credit card registered with PAY.JP
"Teacher, I want to implement a login function in Spring" ① Hello World
How to change app name in rails
Building Rails 6 and PostgreSQL environment with Docker
[Rails withdrawal] Create a simple withdrawal function with rails
Authentication function with Play Framework [Registration and authentication]