blue workbench

Ruby and Rails

【Rails】deviseをやる

WIPです。

gem 'devise'
$ bundle install
$ rails g devise:install
Running via Spring preloader in process 4033
      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 g devise user
Running via Spring preloader in process 4074
      invoke  active_record
      create    db/migrate/20200314142116_devise_create_users.rb
      create    app/models/user.rb
      invoke    rspec
      create      spec/models/user_spec.rb
      invoke      factory_bot
      create        spec/factories/users.rb
      insert    app/models/user.rb
       route  devise_for :users
# frozen_string_literal: true

class DeviseCreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false
      t.string :encrypted_password, null: false

      ## 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
$ rails db:migrate
== 20200314142116 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0306s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0478s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.1920s
-- add_index(:users, :confirmation_token, {:unique=>true})
   -> 0.0666s
== 20200314142116 DeviseCreateUsers: migrated (0.3376s) =======================
$ rails g devise:views users
Running via Spring preloader in process 4811
      invoke  Devise::Generators::SharedViewsGenerator
      create    app/views/users/shared
      create    app/views/users/shared/_error_messages.html.erb
      create    app/views/users/shared/_links.html.erb
      invoke  form_for
      create    app/views/users/confirmations
      create    app/views/users/confirmations/new.html.erb
      create    app/views/users/passwords
      create    app/views/users/passwords/edit.html.erb
      create    app/views/users/passwords/new.html.erb
      create    app/views/users/registrations
      create    app/views/users/registrations/edit.html.erb
      create    app/views/users/registrations/new.html.erb
      create    app/views/users/sessions
      create    app/views/users/sessions/new.html.erb
      create    app/views/users/unlocks
      create    app/views/users/unlocks/new.html.erb
      invoke  erb
      create    app/views/users/mailer
      create    app/views/users/mailer/confirmation_instructions.html.erb
      create    app/views/users/mailer/email_changed.html.erb
      create    app/views/users/mailer/password_change.html.erb
      create    app/views/users/mailer/reset_password_instructions.html.erb
      create    app/views/users/mailer/unlock_instructions.html.erb
group :development do
  gem 'html2haml'
end
$ bundle install
$ find . -name \*.erb -print | sed 'p;s/.erb$/.haml/' | xargs -n2 html2haml
$ find app/views -name \*.erb
app/views/users/unlocks/new.html.erb
app/views/users/passwords/edit.html.erb
app/views/users/passwords/new.html.erb
app/views/users/mailer/confirmation_instructions.html.erb
app/views/users/mailer/reset_password_instructions.html.erb
app/views/users/mailer/password_change.html.erb
app/views/users/mailer/email_changed.html.erb
app/views/users/mailer/unlock_instructions.html.erb
app/views/users/shared/_error_messages.html.erb
app/views/users/shared/_links.html.erb
app/views/users/sessions/new.html.erb
app/views/users/confirmations/new.html.erb
app/views/users/registrations/edit.html.erb
app/views/users/registrations/new.html.erb
$ find app/views -name \*.haml
app/views/layouts/application.html.haml
app/views/layouts/mailer.text.haml
app/views/layouts/mailer.html.haml
app/views/users/unlocks/new.html.haml
app/views/users/passwords/edit.html.haml
app/views/users/passwords/new.html.haml
app/views/users/mailer/unlock_instructions.html.haml
app/views/users/mailer/reset_password_instructions.html.haml
app/views/users/mailer/password_change.html.haml
app/views/users/mailer/confirmation_instructions.html.haml
app/views/users/mailer/email_changed.html.haml
app/views/users/shared/_links.html.haml
app/views/users/shared/_error_messages.html.haml
app/views/users/sessions/new.html.haml
app/views/users/confirmations/new.html.haml
app/views/users/registrations/edit.html.haml
app/views/users/registrations/new.html.haml
$ find app/views -name \*.erb | xargs rm
$ find app/views -name \*.erb