ruby 6.0.0 rails 6.0.3.4 mysql2 0.5.3 devise 4.7.3
Create in the above environment.
① Launch the application you want to create.
console
% rails _6.0.0_new app name-d mysql
(2) Rewrite the description of database.yml in the app.
config/database.yml
#Omission
default: &default
adapter: mysql2
#↓ ↓ Originally encoding: utf8mb4
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
socket: /tmp/mysql.sock
#The following is omitted
③ Enter the gem you want to add to the bottom layer in the gemfile.
Gemfile
#Omission
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'pry-rails'
gem 'devise'
④ Load the gem.
console
% bundle install
console
% yarn install
⑤ Create a database in the directory of the created application on the console.
console
% rails db:create
⑥ Install Devise on the app.
console
% rails g devise:install
(7) Generate a model using Devise. (Migration files and routing are also automatically generated.)
console
#Model name is singular
%rails g devise model name
⑧ Create a view file for new registration and login.
console
% rails g devise:views
⑨ Start the local server.
console
% rails s
⑩ Describe the column to be added to the migration file.
console
% rails db:migrate
Make each edit / setting. view/devise/registrations → Edit new registration screen view/devise/sessions → Edit login screen model/table name.rb → Validation settings
Edit application_controller.rb.
controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
private
def configure_permitted_parameters
#The content of keys is the column name added to the devise table
devise_parameter_sanitizer.permit(:sign_up, keys: [:Column 1, :Column 2, ‥])
end
end
that's all
-The routing to reach the new registration page and login page is automatically generated when the model is created.
-Since the controller processing required for the user management function is hidden behind the scenes, it will be described in "application_controller.rb" that controls the controller.
・ If "rails g devise: install" doesn't work, try the following.
console
$ bundle exec spring stop
Recommended Posts