RailsApp Rails devise 2018年 11月 15日
Rails でログイン機能を実現する場合には,devise という gem を使う. 学内であれば,ldap 認証ができるので,devise-ldap-authentication を使うことが多いのだが,今回は通常のローカル認証での設定を行う. 今回のログイン対象のクラスは Teacher である.
gem 'devise'
$ bundle
Fetching bcrypt 3.1.12
Fetching orm_adapter 0.5.0
Fetching warden 1.2.7
Installing orm_adapter 0.5.0
Installing warden 1.2.7
Installing bcrypt 3.1.12 with native extensions
Fetching responders 2.4.0
Installing responders 2.4.0
Fetching devise 4.5.0
Installing devise 4.5.0
$ bin/rails g devise:install
Running via Spring preloader in process 80494
create config/initializers/devise.rb
create config/locales/devise.en.yml
===============================================================================
Some setup you must do manually if you haven't yet:
1. Ensure you have defined default url options in your environments files. Here
is an example of default_url_options appropriate for a development environment
in config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
In production, :host should be set to the actual host of your application.
2. Ensure you have defined root_url to *something* in your config/routes.rb.
For example:
root to: "home#index"
3. Ensure you have flash messages in app/views/layouts/application.html.erb.
For example:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
4. You can copy Devise views (for customization) to your app by running:
rails g devise:views
===============================================================================
Rails.application.configure do
config.action_mailer.default_url_options = {host: 'localhost', port: 3000}
(中略)
end
%html
(中略)
%body
%p.notice= notice
%p.alert= alert
$ bin/rails g devise:views
/Users/hkob/rails/attendance/vendor/bundle/ruby/2.5.0/gems/railties-5.2.1/lib/rails/railtie/configuration.rb:97:in `method_missing': undefined method `action_mailer' for #<Rails::Application::Configuration:0x00007f81f001ba98> (NoMethodError)
(後略)
require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
$ bin/rails g devise:views
Running via Spring preloader in process 98994
invoke Devise::Generators::SharedViewsGenerator
create app/views/devise/shared
create app/views/devise/shared/_links.html.erb
invoke form_for
create app/views/devise/confirmations
create app/views/devise/confirmations/new.html.erb
create app/views/devise/passwords
create app/views/devise/passwords/edit.html.erb
create app/views/devise/passwords/new.html.erb
create app/views/devise/registrations
create app/views/devise/registrations/edit.html.erb
create app/views/devise/registrations/new.html.erb
create app/views/devise/sessions
create app/views/devise/sessions/new.html.erb
create app/views/devise/unlocks
create app/views/devise/unlocks/new.html.erb
invoke erb
create app/views/devise/mailer
create app/views/devise/mailer/confirmation_instructions.html.erb
create app/views/devise/mailer/email_changed.html.erb
create app/views/devise/mailer/password_change.html.erb
create app/views/devise/mailer/reset_password_instructions.html.erb
create app/views/devise/mailer/unlock_instructions.html.erb
$ env HAML_RAILS_DELETE_ERB=true bin/rails haml:erb2haml
$ bin/rails g devise Teacher
Running via Spring preloader in process 99389
invoke active_record
create db/migrate/20181108090734_devise_create_teachers.rb
create app/models/teacher.rb
insert app/models/teacher.rb
route devise_for :teachers
class Teacher < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
# frozen_string_literal: true
class DeviseCreateTeachers < ActiveRecord::Migration[5.2]
def change
create_table :teachers 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.inet :current_sign_in_ip
# t.inet :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 :teachers, :email, unique: true
add_index :teachers, :reset_password_token, unique: true
# add_index :teachers, :confirmation_token, unique: true
# add_index :teachers, :unlock_token, unique: true
end
end
t.string :name, null: false
$ bin/rails db:migrate
== 20181108090734 DeviseCreateTeachers: migrating =============================
-- create_table(:teachers)
-> 0.0199s
-- add_index(:teachers, :email, {:unique=>true})
-> 0.0112s
-- add_index(:teachers, :reset_password_token, {:unique=>true})
-> 0.0022s
== 20181108090734 DeviseCreateTeachers: migrated (0.0335s) ====================
ビューの部分が終わっていないが今日はここまで