さんさろ

さんさろ

プロダクトエンジニアの雑記&技術、たまにドイツ

ログインは自作しない

gem の devise を使ってみる

rails 触るのまだおっかなびっくりですが、フロントのレイアウトが出来たので、とりあえず管理画面側のログインを作っていきます。

Progate ではログイン機構を手作りしましたが、今回はログイン回りで検索してよく見かける gem の devise というのを使ってみます。

いえ、もちろん自作してもいいんです。いいんですけど、それ全部自作してたらそもそも何でフレームワーク使ってる?ってなりません?(言い訳

どなたかが作ったパッケージを使う方が安心出来るぽんこつですみません。

そういえば devise って普通最後 c だと思うんですけど、なんで devise って s なんですかね?誰か教えて。

 

人様にお教え出来る内容でもないので、私はこんな手順でやりました、こんなエラーでこんな対処しました、という履歴のようなメモです。

 

手順

1.devise をインストール

Gemfileに追記

gem 'devise'

インストール

$ bundle install

出来た。

$ bundle info devise
* devise (4.4.0) Summary: Flexible authentication solution for Rails with Warden Homepage: https://github.com/plataformatec/devise Path: /usr/local/rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/devise-4.4.0

 

2.rails へインストールするコマンドだろうか?を叩くと、
$ rails g devise:install

べろっと何か出て来るので、言われてる通りに設定していく。


2.1 メーラーのルートの設定?

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.

ふむふむ。
in config/environments/development.rb:
ファイルに以下を追加しろと書いてある。vagrantだとどうするんだろうな…?
とりあえず何も追記しないでおいた。


2.2 次はルートを設定。

2. Ensure you have defined root_url to *something* in your config/routes.rb.
For example:
root to: "home#index"

ルートははもう設定してるので飛ばす。

2.3 フラッシュメッセージの設定

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>

これも後でいいや。

2.4 次はコマンド叩いてビューを作る(作ってもらう)

4. You can copy Devise views (for customization) to your app by running:
rails g devise:views

とあるので、これを叩く。

$ rails g devise:views

なんか超いっぱい出来た。

$ rails g devise:views
/usr/local/rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/app_loader.rb:40: warning: Insecure world writable dir /usr/local/rbenv/versions/2.4.3/bin in PATH, mode 040777
Running via Spring preloader in process 14703
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


 あれ、4つある内の結局1つしか実行してないな?

3.ログイン用ユーザのモデルやらマイグレーションやら作成(してもらう)

$ rails g devise User
/usr/local/rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/railties-5.1.4/lib/rails/app_loader.rb:40: warning: Insecure world writable dir /usr/local/rbenv/versions/2.4.3/bin in PATH, mode 040777
Running via Spring preloader in process 15128
invoke active_record
create db/migrate/20180113152748_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

おー。勝手にusersテーブル用のmigrationファイルを作ってくれた。

とりあえずデフォルトのまま migration ファイルを走らせてみる。

$ rails db:migrate

usersテーブルが出来ました。

Viewも作ってくれたっぽい。CSSも何もないから超左に寄ってる。あとはこれを適当に編集していく感じかな。

ためしに Sign up からユーザ登録したら、簡単にユーザ登録出来て、ログイン処理までしてくれてたようだ。

なんだー rails にもコマンド一つで色々やってくれる親切なのあるじゃないですかー。

途中でちょっと怒られたよー

いざログイン画面表示させようとした際に怒られたのが、これ。

undefined method `devise_for' for #<ActionDispatch::Routing::Mapper:0x00007f93d1ad79d0>

んんん…?勝手に route.erb に追加されてたルーティングのメソッドが怒られてる。

これはそもそもインストールした devise が認識されてない?

と思ったら案の定そうだったっぽい。

routing - undefined method `devise_for' in rails action controller - Stack Overflow

apache 再起動で治った。えええーー……そんなに頻繁にwebサーバの再起動が必要なの……dumpautoload 的なものはないの……。
 

参考URL

[*Rails*] deviseの使い方(rails5版) - Qiita