LoginSignup
0
0

顧客マイページ作成

Posted at

はじめに

bootstrap導入済 
namespases使用
devaice導入済
新規顧客登録機能実装済


完成イメージ
スクリーンショット 2023-07-21 23.31.57.png

ルーティング

routes.rb
namespace :public do
  resources :customers, only: [:show, :edit, :update]
end

コントローラー

public/customers_controller作成

rails g controller public/customers show edit update
public/costomers_contoroller.rb
class Public::CustomersController < ApplicationController
  before_action :authenticate_customer!

  def show
    @customer = current_customer
  end

  def edit
    @customer = current_customer
  end

  def update
    @customer = current_customer
    if @customer.update(customer_params)
      redirect_to public_customer_path(@customer), notice: "登録情報が更新されました。"
    else
      render :edit
    end
  end

  private

  def customer_params
    params.require(:customer).permit(:last_name, :first_name, :last_name_kana, :first_name_kana, :postcode, :address, :email)
  end
end

views 顧客マイページ

public/cosutomers/show.html.erb
<div class="container mt-5">
  <div class="row">
    <div class="col-md-2"></div>  
    <div class="col-md-8">

      <h1 class="mb-4">マイページ</h1>

      <h2>登録情報</h2>
      <%= link_to '登録情報編集', edit_customer_path(@customer), class: "btn btn-success mb-4" %>

      <div class="table-responsive">
        <table class="table table-bordered">
          <tr>
            <th class="table-secondary w-25">氏名</th> 
            <td><%= @customer.last_name %> <%= @customer.first_name %></td>
          </tr>
          <tr>
            <th class="table-secondary">カナ</th>
            <td><%= @customer.kana_last_name %> <%= @customer.kana_first_name %></td>
          </tr>
          <tr>
            <th class="table-secondary">郵便番号</th>
            <td><%= @customer.post_code %></td>
          </tr>
          <tr>
            <th class="table-secondary">住所</th>
            <td><%= @customer.address %></td>
          </tr>
          <tr>
            <th class="table-secondary">メールアドレス</th>
            <td><%= @customer.email %></td>
          </tr>
        </table>
      </div>

      <%= link_to '配送先一覧表示', addresses_path, class: "btn btn-info mb-4" %>
      <%= link_to '注文履歴一覧表示', order_path, class: "btn btn-info mb-4" %>

    </div>
    <div class="col-md-2"></div>  
  </div>
</div>

実装方法について

会員側のマイページを作る際には、以下の手順を踏むことが一般的です。

ルーティングの設定: まずはユーザーがマイページにアクセスできるように、適切なURLに対してコントローラーとアクションをマッピングします。たとえば、config/routes.rbファイルで resources :customers, only: [:show] と設定すれば、/customers/:id というURLでマイページにアクセスできます。

コントローラーの作成: 次に、マイページの表示を担当するアクションを作成します。たとえば、CustomersControllerにshowアクションを作成し、その中で @customer = Customer.find(params[:id]) というように、URLからIDを取得して該当する顧客を検索します。

ビューの作成: 最後に、顧客の情報を表示するビューを作成します。たとえば、app/views/customers/show.html.erbにマイページの内容を書き込むことで、顧客の情報を表示できます。

なお、ログイン状態を管理するためにDeviseを使っている場合、現在ログインしているユーザーの情報はcurrent_userメソッドで取得できます。そのため、特定の顧客のIDをURLから取得するのではなく、@customer = current_userとして現在ログインしている顧客の情報を表示することも可能です。

以上が基本的な手順ですが、具体的な実装は使用しているフレームワークやライブラリ、要件により異なるため、適宜調整が必要です。

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