Tutorial on Ruby on Rails to implement the most basic user registration and login functions, rubyrails

Source: Internet
Author: User

Tutorial on Ruby on Rails to implement the most basic user registration and login functions, rubyrails

It is very convenient to implement the user registration and login functions in Rails. For example, you can use the Devise class to implement the complete function of the gem extension package. You can also use the has_secure_password that comes with Rails to create a new password. The following describes how to use has_secure_password to implement user registration and logon.

Preparations

Create a project:

rails new user_login

Bcrypt is required for the encryption function in has_secure_password, so you need to enable the bcrypt gem package in the project. Go to the project directory and modify the following Gemfile:

# Use ActiveModel has_secure_passwordgem 'bcrypt', '~> 3.1.7'

Save and exit. Run the bundle install command to install the newly enabled gem package.

Create User Module

To operate and manage user information, you must first create and save the user's data table and model:

rails g model user name:string password_digest:string

The password_digest field is used to save the password string after encryption obfuscation. It must be provided and cannot be changed to another name. Otherwise, has_secure_password cannot be used normally.

Then introduce the has_secure_password function in the user module:

# app/models/user.rbclass User < ActiveRecord::Base has_secure_passwordend

Create a user data table:

rake db:migrate

Implement registration

Create an Applicant (Applicant) controller to process user registration:

rails g controller applicants new create

The applicants Controller provides two methods:

  • New: used to process the registration interface
  • Create: used to save Registration Information

The Controller method created by the above command uses get requests by default. The create Method for saving Registration Information uses the post request. Therefore, modify the following content in config/routes. rb:

post 'applicants/create'

Register the controller:

# app/controllers/applicants_controller.rb

class ApplicantsController < ApplicationController
 def new
  @user = User.new
 end

 def create
  @user = User.create(user_params)
  if @user.save
   redirect_to :sessions_new
  else
   render "new"
  end
 end

 private
  def user_params
   params.require(:user).permit(:name, :password, :password_confirmation)
  end
end

Complete the registration interface:


<!-app / views / applicants / new.html.erb->

<h1> Sign up </ h1>

<% if @ user.errors.any?%>
<ul>
   <% @ user.errors.full_messages.each do | message |%>
   <li> <% = message%> </ li>
   <% end%>
</ ul>
<% end%>

<% = form_for @user, url:: applicants_create do | f |%>
   <p>
     <% = f.label: name%>
     <% = f.text_field: name%>
   </ p>

   <p>
     <% = f.label: password%>
     <% = f.password_field: password%>
   </ p>

   <p>
     <% = f.label: password_confirmation%>
     <% = f.password_field: password_confirmation%>
   </ p>

   <p> <% = f.button "Submit"%> </ p>
<% end%>



This simply implements the registration function.

Implement login function

Create a Session controller to handle user login and logout:

rails g controller sessions new create
Here are 2 methods created by default on the sessions controller:

new: used to handle the login interface
create is used to handle the login process
As with registration, you need to change the default route for create to post:

# config / routes.rb

post 'sessions / create'

Complete the functions of the session controller:

# app/controllers/sessions_controller.rb

class SessionsController < ApplicationController
 def new
 end

 def create
  user = User.find_by(name: user_params[:name]).try(:authenticate, user_params[:password])
  if user
   render plain: sprintf("welcome, %s!", user.name)
  else
   flash.now[:login_error] = "invalid username or password"
   render "new"
  end
 end

 private
  def user_params
   params.require(:session).permit(:name, :password)
  end
end



Complete the session login interface:


<!-app / views / sessions / new.html.erb->

<h1> Sign in </ h1>

<% if flash [: login_error]%>
   <p> <% = flash [: login_error]%> </ p>
<% end%>

<% = form_for: session, url:: sessions_create do | f |%>

   <p>
     <% = f.label: name%>
     <% = f.text_field: name%>
   </ p>

   <p>
     <% = f.label: password%>
     <% = f.password_field: password%>
   </ p>

   <p> <% = f.button "Login"%> </ p>

<% end%>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.