LoginSignup
1
2

More than 5 years have passed since last update.

Rails - deviseやってみる

Posted at

参考:[Rails] deviseの使い方(rails5版)

上記のサイトを見ながらやってみます。

インストール・設定

gem追加してインストールしました。

下記を実行しました。2つのファイルが作成されました。設定ファイルと翻訳関連ファイルでしょうか?

$ rails g devise:install
create  config/initializers/devise.rb
create  config/locales/devise.en.yml

みながら進めて2のデバイスの設定まで終わりました。

3のユーザモデルの作成が、今回customersテーブル使いたいので困りました。usersテーブルに変更することにしました笑

ユーザモデル作成

$ rails g devise User
unning via Spring preloader in process 88748
      invoke  active_record
      create    db/migrate/20170428143630_devise_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      insert    app/models/user.rb
       route  devise_for :users

ユーザモデルで利用したい機能を設定できるらしい。今回はtwitter認証とかいらないし、シンプルでいいから下記のようにしてみた。

devise :database_authenticatable, :lockable, 
       :recoverable, :validatable, 

利用機能に合わせてマイグレーションファイルも調整します。

20170428143630_devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration[5.0]
  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

db:migrateします。

rails db:migrate

動作確認

一旦ここでどうなってるか確認してみます。

routes.rbにdevise_for :usersというのがあります。

Prefix Verb   URI Pattern                    Controller#Action
    new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
        user_session POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
   new_user_password GET    /users/password/new(.:format)  devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
       user_password PATCH  /users/password(.:format)      devise/passwords#update
                     PUT    /users/password(.:format)      devise/passwords#update
                     POST   /users/password(.:format)      devise/passwords#create
     new_user_unlock GET    /users/unlock/new(.:format)    devise/unlocks#new
         user_unlock GET    /users/unlock(.:format)        devise/unlocks#show
                     POST   /users/unlock(.:format)        devise/unlocks#create
                root GET    /                              pages#index
         pages_index GET    /pages/index(.:format)         pages#index
          pages_show GET    /pages/show(.:format)          pages#show

http://localhost:3000/users/sign_inにアクセスするとログイン画面が表示されます。でも今回ユーザがユーザ登録できないので、手動でユーザ登録してみます。

手動でユーザ登録

$ rails c
$ user = User.new
$ user.email = "hoge@gmail.com"
$ user.password = "12345678"
$ user.save

上記でログインしたらログインできました。

アクセスコントロール

次に、どのページを誰に見せるかというのをやってみたいと思います。どうも、cancanという雑誌みたいなやつが人気らしいです。長そうだし、管理画面の作成gemも確認したいので、一旦終了。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2