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!
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
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
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 end
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.
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.
%header - if user_signed_in? %div = link_to "Log out", destroy_user_session_path, method: :delete - else %div = 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' end
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.
% 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 end end
It's a dull screen, but if all goes well, it should look like the one below.
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
・ Enter your email address and password and click "sign up"
・ Go to the top page and log out will be displayed. Click log out.
・ I was able to log out, so click Login.
・ Enter your email address and password and click Login
-Transition to the top page and logout is displayed.
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!