0
0

More than 1 year has passed since last update.

管理者側 顧客情報詳細ページ

Posted at

はじめに
bootstrap導入済 
namespases使用
devaice導入済
顧客一覧ページ作成済
Admin::CustomersController作成済


完成イメージ

ルーティング

costomersにshow
編集ページのためのedit
注文履歴一覧を表示するためにresources :orders, only: [:index] を追加

 namespace :admin do
  resources :customers, only: [:show, :edit, :update] do
+   resources :, only: [:index] # ここを追加
  end

モデル

顧客が多数の注文を持っているようにします
顧客の注文履歴一覧を見るためです

costomer.rb
class Customer < ApplicationRecord
   has_many :orders
end
order.rb
class Order < ApplicationRecord
   belongs_to :customer
end

:shamrock:コントローラー

特定の顧客の情報を一つ取得して、@customerに格納します。

admin/costomers_contoroller.rb
def show
  @customer = Customer.find(params[:id])
end
admin/orders_controller.rb
class Admin::OrdersController < ApplicationController
  def index
      @customer = Customer.find(params[:customer_id])
      @orders = @customer.orders
   end
end

@customer = Customer.find(params[:customer_id])
URLから送られてきた顧客ID(customer_id)を使って、その顧客の情報をデータベースから取得しています。この顧客情報は、後でビューで表示するために @customer 変数に格納されます。

@orders = @customer.orders
取得した顧客が過去に行ったすべての注文情報をデータベースから取得しています。これらの注文情報は、後でビューで表示するために @orders 変数に格納されます。

つまり、このコントローラーは、指定された顧客が過去に行ったすべての注文の一覧を取得し、それをビューに表示するための情報を提供する役割を果たしています。

views 顧客情報詳細ページ

admin/costomers/show.html.erb
<div class="container mt-5">
  <h2 class="mb-4 text-center"><%= @customer.last_name %> <%= @customer.first_name %>さんの会員詳細</h2>

  <div class="pl-5">
    <p class="my-3"><strong>会員ID </strong><span style="margin-left:150px;"><%= @customer.id %></span></p>
    <p class="my-3"><strong>氏名 </strong><span style="margin-left:160px;"><%= @customer.last_name %> <%= @customer.first_name %></span></p>
    <p class="my-3"><strong>フリガナ </strong><span style="margin-left:130px;"><%= @customer.kana_last_name %> <%= @customer.kana_first_name %></span></p>
    <p class="my-3"><strong>郵便番号 </strong><span style="margin-left:130px;"><%= @customer.post_code %></span></p>
    <p class="my-3"><strong>住所 </strong><span style="margin-left:160px;"><%= @customer.address %></span></p>
    <p class="my-3"><strong>電話番号 </strong><span style="margin-left:125px;"><%= @customer.phone_number %></span></p>
    <p class="my-3"><strong>メールアドレス </strong><span style="margin-left:80px;"><%= @customer.email %></span></p>
    <p class="my-3"><strong>会員ステータス </strong><span style="margin-left:90px; color:<%= @customer.is_deleted ? 'gray' : 'green' %>;"><%= @customer.is_deleted ? '退会済み' : '有効' %></span></p>

   <%= link_to '編集する', edit_admin_customer_path(@customer), class: 'btn btn-success btn-space' %>
   <%= link_to '注文履歴一覧へ', admin_customer_orders_path(@customer), class: 'btn btn-info' %>


  </div>
</div>
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