[RUBY] Make a login function with Rails anyway

Let's create a login function anyway

This article is only for creating a login function in a WEB application using Rails. Let's memorize this in the body and be able to make it at explosive speed!

Make an app template

If you don't make this, nothing will start! Execute the following command from the terminal.

% rails _5.2.3_new app name-d mysql

Once you have created the app template with the above command, move to the app directory.

%cd app name

install devise

Open the Gemfile in an editor and add devise. devise is a gem that makes it easy to create the functions required for user authentication. Also, this time I use haml instead of erb, so I also install a gem that converts erb files to haml.

gem 'devise'
gem "haml-rails"

Add devise to Gemfile and install it!

% bundle install

After the installation is complete, convert the erb file to haml.

% rails haml:erb2haml

After completing the above conversion work, create a devise configuration file.

% rails g devise:install

Then, the following configuration file is created. config/initializers/devise.rb config/locales/devise.en.yml

Settings around the database

Then execute the following command to create the user model.

% rails g devise user

The routing file will now also contain devise's routing.

Rails.application.routes.draw do
  devise_for :users

The database used by this app has not been created yet, so let's create it.

% rails db:create 

I think that the migration file was created when the model was created, so let's migrate xxxxxxxxxxxxxx_devise_create_users.rb

% rails db:migrate

This completes the settings around the database.

Display the top page

Next, create a view file for the top page. Let's make a lead to login, new registration, and logout here. This time, we will create index.html.haml in app / views / posts assuming that the app allows users to post some posts. After installing devise, you can use a helper method called user_signed_in, so let's separate the display when not logged in and when logged in.

  - if user_signed_in?
      = link_to "Log out", destroy_user_session_path, method: :delete
  - else
      = link_to "Login", new_user_session_path
      = link_to "sign up", new_user_registration_path

When the top page is created, add the routing to routes.rb.

Rails.application.routes.draw do
  devise_for :users
  root to: 'posts#index'

At the moment, when I try to open the top page, I think that the following error will occur, so I will make a controller.

スクリーンショット 2020-05-23 19.56.32.png

% rails g controller posts

Now you have a controller! You should be able to open the top page by adding the index action to the created controller.

class PostsController < ApplicationController
  def index

It's a dull screen, but if all goes well, it should look like the one below. スクリーンショット 2020-05-23 20.03.48.png

Login and new registration

Next, log in and create a new registration screen. Execute the following command to create a view for new registration and login.

% rails g devise:views

.New registration screen app/views/devise/registrations/new.html.erb

·Login screen app/views/devise/sessions/new.html.erb

This time it will be created with haml, so let's execute the following command again to convert erb to haml.

% rails haml:erb2haml

If you can do so far, you should be able to newly register, log in, and log out, so I will try it immediately. ・ Click new registration スクリーンショット 2020-05-23 20.03.48.png

・ Enter your email address and password and click "sign up" スクリーンショット 2020-05-23 20.11.41.png

・ Go to the top page and log out will be displayed. Click log out. スクリーンショット 2020-05-23 20.15.26.png

・ I was able to log out, so click Login. スクリーンショット 2020-05-23 20.17.52.png

・ Enter your email address and password and click Login スクリーンショット 2020-05-23 20.19.00.png

-Transition to the top page and logout is displayed. スクリーンショット 2020-05-23 20.22.42.png

It looks awkward because the style isn't right, but for the time being, the server-side implementation is complete! Please refer to this article and aim for explosive implementation!

Recommended Posts

Make a login function with Rails anyway
Let's make a search function with Rails (ransack)
Login function implementation with rails
Make a site template easily with Rails
I tried to make a group function (bulletin board) with Rails
How to make a follow function in Rails
Introduced graph function with rails
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
I tried to make a login function in Java
Login function with Spring Security
[Rails] Make a breadcrumb trail
I want to make a function with kotlin and java!
[Ruby on Rails] Implement login function by add_token_to_users with API
Implement login function simply with name and password in Rails (3)
Make a digging maze with Ruby2D
[Rails] Make pagination compatible with Ajax
Add a search function in Rails.
Make a slideshow tool with JavaFX
Implemented mail sending function with rails
[Rails] Creating a new project with rails new
Make a Christmas tree with swift
Create pagination function with Rails Kaminari
Implement simple login function in Rails
Make a garbage reminder with line-bot-sdk-java
Make a list map with LazyMap
Make a typing game with ruby
I want to add a browsing function with ruby on rails
Login function
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
Let's make a Christmas card with Processing!
Make a family todo list with Sinatra
[Rails6] Create a new app with Rails [Beginner]
Rails Addition of easy and easy login function
Try to implement login function with Spring-Boot
Make a family todo list with Sinatra
Let's make a smart home with Ruby!
[docker] [nginx] Make a simple ALB with nginx
[Rails 5] Create a new app with Rails [Beginner]
Let's make an error screen with Rails
Create a login function using Swift's Optional
[Rails] Implement the product purchase function with a credit card registered with PAY.JP
Create a SPA with authentication function with Rails API mode + devise_token_auth + Vue.js 3 (Rails edition)
Implementation of Ruby on Rails login function (Session)
[Rails] rails new to create a database with PostgreSQL
[Rails 6] Ranking function
Add a tag function to Rails. Use acts-as-taggable-on
Try to implement login function with Spring Boot
Create a team chat with Rails Action Cable
[Rails] Category function
[Rails] I made a draft function using enum
Rails follow function
Rails6 I want to make an array of values with a check box
Make System.out a Mock with Spock Test Framework
[Note] Summary of rails login function using devise ①
I made a LINE bot with Rails + heroku
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
I made a portfolio with Ruby On Rails
[Rails] Notification function
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
Run Scala with GraalVM & make it a native image
How to make batch processing with Rails + Heroku configuration