ログインページのカスタマイズ
本章ではログインページ /users/sign_in
で使われる view ファイルを編集し、デザインを調整します。
変更前 view ファイルの確認
devise のコマンドで作成した直後ですと、ログイン用の view ファイルは以下のようになっています。
(変更前): app/views/devise/sessions/new.html.erb
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "current-password" %>
</div>
<% if devise_mapping.rememberable? %>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
また、現時点の見た目はこのようになっています。
<img width="605" alt="image" src="https://user-images.githubusercontent.com/68495563/180606063-42566087-756b-452b-a78a-4b1aad3e7e3a.png">
ユーザー登録ページと同じく、使いやすい見た目ではないのでこちらも修正していきましょう。
view の修正
では、ログインの view を次のように変更してください。
(変更後): app/views/devise/sessions/new.html.erb
<%= form_with scope: resource, as: resource_name, url: session_path(resource_name), class: "space-y-6 w-1/2 max-w-sm", local: true do |f| %>
<label class="block text-xl font-bold text-gray-700">ログイン</label>
<div class="mt-1">
<label class="text-gray-700 text-md">
メールアドレス
</label>
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 mt-1 block w-full sm:text-sm placeholder-gray-400 border border-gray-300 rounded-md", placeholder: "test@example.com" %>
</div>
<div class="mt-1">
<label class="text-gray-700 text-md">
パスワード
</label>
<%= f.password_field :password, autocomplete: "password", class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 mt-1 block w-full sm:text-sm placeholder-gray-400 border border-gray-300 rounded-md", placeholder: "pass1234" %>
</div>
<button type="submit" class="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
<%= f.submit "ログイン" %>
</button>
<% end %>
画面確認
では開発用サーバを起動し、ブラウザで確認していきましょう。
bin/dev
コマンドで開発用サーバを起動します。
$ bin/dev
09:33:09 web.1 | started with pid 7086
09:33:09 css.1 | started with pid 7087
09:33:11 web.1 | => Booting Puma
09:33:11 web.1 | => Rails 7.0.3 application starting in development
09:33:11 web.1 | => Run `bin/rails server --help` for more startup options
09:33:11 web.1 | Puma starting in single mode...
09:33:11 web.1 | * Puma version: 5.6.4 (ruby 3.0.4-p208) ("Birdie's Version")
09:33:11 web.1 | * Min threads: 5
09:33:11 web.1 | * Max threads: 5
09:33:11 web.1 | * Environment: development
09:33:11 web.1 | * PID: 7086
09:33:11 web.1 | * Listening on http://127.0.0.1:3000
09:33:11 web.1 | * Listening on http://[::1]:3000
09:33:11 web.1 | Use Ctrl-C to stop
09:33:12 css.1 |
09:33:12 css.1 | Rebuilding...
09:33:12 css.1 | Done in 201ms.
サーバを起動できたら、ブラウザで http://localhost:3000/users/sign_in
にアクセスします。
<img width="460" alt="image" src="https://user-images.githubusercontent.com/68495563/180606334-00a049d3-253a-4040-a151-add77a1bbf59.png">
このような画面になっていれば OK です。
※前回のレッスンでユーザー登録を試していた場合、ログイン状態が残りトップにリダイレクトされてしまいます。
その場合、Chrome のシークレットウィンドウを活用してください。
(この後のカリキュラムでログアウトボタンを実装することで解消します。)
テスト修正
ユーザー認証機能関連の System Spec は前回作成した spec/system/users_spec.rb
で扱っているので、ログイン機能のテスト機能についても同じファイルに追記します。
describe 'ユーザー登録機能の検証' の
ブロック (do ~ end) の下にテストを追加していきます。
spec/system/users_spec.rb
...
context 'passwordとpassword_confirmationが一致しない場合' do
let(:password_confirmation) { "#{password}hoge" } # passwordに"hoge"を足した文字列にする
it 'ユーザーを作成せず、エラーメッセージを表示する' do
expect { subject }.not_to change(User, :count)
expect(page).to have_content("Password confirmation doesn't match Password")
end
end
end
end
####### ここから追記 #######
describe 'ログイン機能の検証' do
# 事前にユーザー作成
before do
create(:user, nickname: nickname, email: email, password: password, password_confirmation: password) # 事前にユーザー作成
visit '/users/sign_in'
fill_in 'user_email', with: email
fill_in 'user_password', with: 'password'
click_button 'ログイン'
end
context '正常系' do
it 'ログインに成功し、トップページにリダイレクトする' do
expect(current_path).to eq('/')
end
end
context '異常系' do
let(:password) { 'NGpassword' }
it 'ログインに失敗し、ページ遷移しない' do
expect(current_path).to eq('/users/sign_in')
end
end
end
テスト実行
では、すべてのテストに通ることを確認しましょう。
$ bin/rspec
...
ログイン機能の検証
正常系
ログインに成功し、トップページにリダイレクトする
異常系
ログインに失敗し、ページ遷移しない
Finished in 6.06 seconds (files took 0.42416 seconds to load)
16 examples, 0 failures
コケるテストがないことを確認できれば OK です。
変更をコミット
ここまでの変更をコミットして完了です。
$ git add .
$ git commit -m "ログイン画面の修正"
$ git push