About devise

At first

** Day 18 of Calendar Planning 2020 ** It's been about 3 months since I started studying programming, so I will leave a note of what I learned in the article as an output. I would be happy if I could help anyone entering the world of programming. Please let me know if there are any words that are wrong, wrong, or misunderstood ^^ I'm sorry if it's hard to read the words for a long time. I will do my best to get used to it little by little.

About gem

There is a convenient thing called gem in Ruby. You can create the function from scratch by yourself, but it is complicated and difficult, so you can easily add the function to the application without such trouble. You can speed up development. (Note: I was told that it is not good to rely too much ^^)

One of the gems is "devise", which we will cover this time.

What is devise?

What exactly does devise do?

Do you not register as a user when you often use Internet services? New registration, login, etc.

It's a gem that can do it!

It seems that you can create such a function from scratch, but you can easily add it as a function by using this!

How to introduce

First from installation

Gemfile


.
.
.
.
.
.


gem "devise"

I think that a Gemfile was created when you created the application. Open it and add the above at the bottom.

Terminal


$ bundle install

Bundle is also a kind of gem, but unlike devise, it manages gems instead of adding functions.

Terminal


$ rails g devise:install

This will do the initial setup of devise.

Preparation for introduction is now complete

Try to display the login screen

Create a user table. Normally, the table is created from 1, but if you use devise, it will be created automatically.

Terminal


$rails g devise model name#Usually rails g model model name, but devise version is on the left

Then a migration file is generated. I wonder if it looks like this ...

def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.string   :current_sign_in_ip
      # t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

This is the default. If there is no particular problem, it will remain as it is Please add the user's name and address and necessary column names in the table according to the application.

When the postscript is over,

Terminal


$ rails db:migrate

Other You should also find devise settings in config/routes.rb and app/model/model name.rb.

In fact, you can now use the login page! ^^

Supplement

If you want to change the layout of the login page

Terminal


$ rails g devise:views #If you want to set the file name, write it after views

When you add login information

By default, only your email address and password are available. In the actual application, I want to include the name etc. in the login conditions! I use it sometimes

app/controllers/application_controller.rb



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

  protected

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

before_action :configure_permitted_parameters, if: :devise_controller?

Before using the devise function, we will process configure_permitted_parameters! Defined as The contents defined below are executed.

devise_parameter_sanitizer.permit A method that allows data manipulation. Line 1: Name data manipulation is allowed when signing up. 2nd line: Name data manipulation is allowed when signing in. 3rd line: Data manipulation of name and email is permitted when updating account information.

protected This is similar to the strong parameter. The slight difference is that it can be referenced even if it is called from another controller. private This is only in the controller described.

Finally

devise is very useful. I hope you can make good use of it.

I remember adding the devise controller, so I think I can customize it more. I think the only way to add files was to change views to controllers. (I'm sorry if it's different)

Recommended Posts

About devise
About =
[rails] About devise defaults
About method.invoke
About Kotlin
About attr_accessor
Introducing devise
About methods often used in devise
About Hinemos
About inheritance
About params
About Docker
About Rails 6
About form_for
About Spring ③
About enum
About polymorphism
About Optional
About JitPack
About Dockerfile
About encapsulation
About Docker
About JAVA_HOME
About active_hash
About static
About exceptions
Devise procedure
[Rails] devise
Introducing devise
About scope
[Maven] About Maven
About exception handling
Review about bundler
[Java] About Java 12 features
About Rails routing
About cyclomatic complexity
About exception handling
About AsyncTask Generics
About Ruby symbols
About array multiplication
[Java] About arrays
About HotSpot VM
About ruby ​​form
About Active Storage
About Spring AOP
About singular methods
About getter setter
About build tools
About MacinCloud introduction
[Note] About nil
About keyword arguments
devise user registration
Chew about API
[About double hash]
About installing Cocoapods
Something about java
Where about java
About HttpServlet () methods
About ActiveRecord :: PendingMigrationError
About SIGNING_REGION: NoSuchFieldError
[Rails] Introducing devise