** 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.
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 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!
. . . . . . gem "devise"
I think that a Gemfile was created when you created the application. Open it and add the above at the bottom.
$ bundle install
Bundle is also a kind of gem, but unlike devise, it manages gems instead of adding functions.
$ rails g devise:install
This will do the initial setup of devise.
Preparation for introduction is now complete
Create a user table. Normally, the table is created from 1, but if you use devise, it will be created automatically.
$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,
$ rails db:migrate
You should also find devise settings in
In fact, you can now use the login page! ^^
$ rails g devise:views #If you want to set the file name, write it after views
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
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.
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.
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)