0
0

More than 1 year has passed since last update.

退会確認画面 退会ステータス

Last updated at Posted at 2023-07-21

はじめに
bootstrap導入済 
namespases使用
devaice導入
:blossom:boolean型で実装


今回の完成イメージ
スクリーンショット 2023-07-20 23.47.53.png

:cherry_blossom: boolean型について

真偽値を表現するためのデータ型です。これは、主に論理的な真(true)または偽(false)のいずれかの値を持つことができます。

データベースでの使用例としては、ユーザーが商品の購入を希望しているかどうか、商品が在庫切れかどうかなど、二つの状態のうちどちらかを示す必要がある場合に使用されます。

Railsでは、これをデータベースのカラムに適用して、そのカラムがtrueまたはfalseの値を持つことができます。例えば、今回のis_deletedフィールドは、顧客が退会したかどうかを示すためにBoolean型が使用されています。

ルーティング

routes.rb
 resources :customers, only: [:show, :edit, :update] do
      collection do
        get 'check_out'
        get 'withdraw'
        patch 'withdraw_update'
      end

コントローラー

sessions_controller.rb
 before_action :withdraw, only: [:create]

    protected

   def withdraw
        @customer = Customer.find_by(email: params[:customer][:email])
      if @customer&.valid_password?(params[:customer][:password]) && @customer.is_deleted
          redirect_to new_customer_session_path, notice: 'アカウントは退会済みです。'
      end
   end

退会済みのユーザーをログインさせないためのコードです。このコードでは、メールアドレスとパスワードのチェックを行い、ユーザーが退会済みであればログインページにリダイレクトしています。

public/customers_controller.rb
class Public::CustomersController < ApplicationController
   before_action :authenticate_customer!

  def withdraw
  end

  def withdraw_update
    current_customer.update(is_deleted: true)
    reset_session
    redirect_to root_path
  end
end

do と end の間には、そのリソースのコレクション(すなわち、全顧客)に対する追加のルートが定義されています。具体的には、以下のルートが作られます:

check_out へのGETリクエスト
withdraw へのGETリクエスト
withdraw_update へのPATCHリクエスト

これらのルートは、全顧客に対する特定の操作を表す可能性があります。例えば、顧客全体のチェックアウト状態を確認したり、全顧客からの引き出しを行ったり、引き出しの更新を行ったりするなどです。

views 退会確認画面

costomers/withdraw.html.erb
<div class="container text-center">
  <h1 class="my-4">本当に退会しますか?</h1>
  
  <p>退会すると、会員登録情報や</p>
  <p>これまでの購入履歴が閲覧できなくなります。</p>
  <p>退会する場合は、「退会する」をクリックしてください。</p>

  <div class="d-flex justify-content-center mt-5">
    <%= button_to '退会しない', customer_path(current_customer), method: :get, class: 'btn btn-primary' %>
    <div class="custom-gap"></div>
    <%= button_to '退会する', withdraw_update_customers_path, method: :patch, class: 'btn btn-danger', data: { confirm: '本当に退会しますか?' } %>
  </div>
</div>

:star:ボタンの間隔を開けるためにcss追加

customers.scss
.custom-gap {
    width: 100px;
}
0
0
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
0
0