devise user registration

1 Install devise

To implement the user registration function, install a gem called devise.

  1. Add gem'devise' in Gemfile

  2. bundle install in the terminal

  3. Here, it will not be reflected unless the server is started with rails s

  4. Create a configuration file with rails g devise: install

2 Create user model + table

The point to note here is that it is not the same as when making a normal model.

Modeling

--Enter rails g devise user in the terminal (at this time, routing is automatically set and migration file is also generated)

class DeviseCreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :name, null: false
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

Create table

--Create a table with the rails db: migrate command in the terminal

3 Sign up (new registration)

The information you enter when signing up is sent to the server as a parameter. Normally, the strong parameters of the controller limit the parameters received, but when devise, the writing method is different.

devise_parameter_sanitizer method

Parameters can be obtained from requests such as "login" and "new registration" related to devise's User model. The description below allows a key parameter called a nickname when signing up. In fact, the items required for new registration will be added after the nickname. Note that the arguments in permit here are different from normal parameters!


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  private

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
  end
end

The first before_action is the setting to execute the configure_permitted_paramaters method if it is a process by devise. You need a whole thing, but you don't have to remember it all because it will come out when you look up this area.

As a result, the information at the time of new registration can be saved in the table.

Recommended Posts

devise user registration
[For beginners] Test devise user registration with RSpec
Memorandum [Rails] User authentication Devise
Edit user information with Devise
Implement user registration function and corporate registration function separately in Rails devise
Implement user management functionality using Devise
6 points to doubt when user registration is not possible with devise
Introducing devise
Implementation of user authentication function using devise (2)
Creating a user authentication function using devise
Implementation of user authentication function using devise (1)
About devise
Implementation of user authentication function using devise (3)
Devise procedure
[Rails] devise
Introducing devise
Use [Rails] devise Guest user function (for portfolio)
Implement user edit / update function without using devise