Railsチュートリアル8章 セッション

Railsチュートリアル8章について大事なとこメモっときます!

テストコードはRspecで書いてます。

 

Sessionとは・・・

ログイン情報を保持する。

sessionはブラウザを閉じると自動で終了する。

Google Chromeの場合は、ブラウザを閉じてもログイン情報保持されたが、これはあくまでブラウザ依存。他のブラウザでは保持しない場合がある。その為次にクッキーを設定する必要がある。(これは次章)

---------------------------------------------------------------------------------------

 

flashメッセージ

エラーメッセージはActive Recordによって自動生成されていたが(User新規登録時の情報が正しくない時)、セッションはActive Recordではないのでエラーメッセージを表示しない。

そこでflashでエラーを表示する。

 

flashを使うにおいて renderとredirectの違い

ーーーーーーーーーーーーー

・renderの場合

flash[:danger]

render ___

このままではだめ。他のページに行ってもエラー文(flash)が出る。これはrenderされているから。

flash.now[:danger]に変更する必要がある。

こうするとその後リクエストが発生した時にエラー文は消える

 

・一方redirectの場合は

flash[:success]

redirect to _____

リダイレクトされるのでこのままでいい。

---------------------------------------------------------------------------------------

 

form_with セッションの場合

今まではモデルで使ってきた。

例)users/new

<%= form_with(model: @user, local: true) do |f| %>

 

sessionにはモデルがない。

しかしurlscopeを置けば問題なく使える

<%= form_with(url: login_path, scope: :session, local: true) do |f| %>

 

--------------------------------------------------------------------------------------

digest    

この章では出てきたが使わなかった。しかし次章以降で大事になるので。

user.rb

def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost) ここが大事!!
 
BCrypt::Password ハッシュ化
# 渡された引数をハッシュにしている(string)
# このbcryptというのはhas_secure_passwordを使うために入れたgemである。
# よってやってること自体は一緒
end

          

 -------------------------------------------------------------------------------------

テストコード(Rspec)

spec/system/sessions_spec.rb

require 'rails_helper'

RSpec.describe "Sessions", type: :system do
before do
@user = FactoryBot.create(:user)
end

it 'should get new' do
visit login_path
expect(current_path).to eq(login_path)
end

it "login with invalid information" do
visit login_path
fill_in 'Email', with: ""
fill_in 'Password', with: ""
 
find('input[name="commit"]').click
 
expect(page).to have_content("Invalid email/password combination")
visit help_path
expect(page).not_to have_content("Invalid email/password combination")
end

it "login with valid information" do
visit login_path
fill_in 'Email', with: @user.email
fill_in 'Password', with: @user.password
find('input[name="commit"]').click
expect(current_path).to eq(user_path(@user))
find(".dropdown-toggle").click
# ドロップダウンをクリック クラス名
expect(page).to have_link href: logout_path
expect(page).to have_link href: user_path(@user)
expect(page).not_to have_link href: login_path
end

it "login with valid email/invalid password" do
visit login_path
fill_in 'Email', with: @user.email
fill_in 'Password', with: "abcdef"
 
find('input[name="commit"]').click
 
expect(page).to have_content("Invalid email/password combination")
visit help_path
expect(page).not_to have_content("Invalid email/password combination")
end

it "login with valid information followed by logout" do
visit login_path
fill_in 'Email', with: @user.email
fill_in 'Password', with: @user.password
find('input[name="commit"]').click
expect(current_path).to eq(user_path(@user))
find(".dropdown-toggle").click
 
expect(page).to have_link href: logout_path
click_link 'Log out'
# リンクをクリックする
expect(current_path).to eq(root_path)
expect(page).to have_link href: login_path

 
end
end

 

   新規登録時にログインしている(追加)

spec/system/users_spec.rb

it "valid signup information" do
visit signup_path
fill_in 'Name', with:"111"
fill_in 'Email', with:"111@111.com"
fill_in 'Password', with: "101010"
fill_in 'Confirmation', with: "101010"
expect do
find('input[name="commit"]').click
end.to change { User.count }.by(1)

expect(has_css?('.user_info')).to be_truthy
# showページにあるCSS これなくてもいいよ

expect(page).to have_content'Welcome to the Sample App!'
ここから追加!!
# ログインしているかどうかは
# ログアウト等があることで証明できると思う
find(".dropdown-toggle").click
# ドロップダウンをクリック クラス名
expect(page).to have_link href: logout_path
# expect(page).to have_link href: user_path(@user) これはだめか
expect(page).not_to have_link href: login_path

 

以上で8章を終わります!