学生向けプログラミング入門 | 無料

学生向けにプログラミングを無料で解説。Java、C++、Ruby、PHP、データベース、Ruby on Rails, Python, Django

Rails6.0 | 仕事売買サイトの構築 | 26 | 注文コントローラとビュー

[25]注文モデル << [ホームに戻る] >> [27]注文の確認


注文コントローラを作成していきます。


「app\controllers」フォルダに「orders_controller.rb」ファイルを新規作成してください。


app\controllers\orders_controller.rb(新規作成したファイル)

class OrdersController < ApplicationController
    before_action :authenticate_user!
    
    def create
        gig = Gig.find(params[:gig_id])
        pricing = gig.pricings.find_by(pricing_type: params[:pricing_type])

        if (pricing && !gig.has_single_pricing) || (pricing && pricing.basic? && gig.has_single_pricing)
            charge(gig, pricing)
        else
            flash[:alert] = "価格が間違っています。"
        end

        redirect_to request.referrer
    end

    def selling_orders
        @orders = current_user.selling_orders
    end

    def buying_orders
        @orders = current_user.buying_orders
    end

    private

    def charge(gig, pricing)
        order = gig.orders.new
        order.due_date = Date.today() + pricing.delivery_time.days
        order.title = gig.title
        order.seller_name = gig.user.full_name
        order.seller_id = gig.user.id
        order.buyer_name = current_user.full_name
        order.buyer_id = current_user.id
        order.amount = pricing.price

        if order.save
            flash[:notice] = "発注しました。"
        else
            flash[:alert] = order.errors.full_messages.join(', ')
        end
    end
end



ルートの設定をします。


記述追加 config\routes.rb
22行目に「resources :orders, only: [:create]」の記述を追加しています。

Rails.application.routes.draw do

  # ルートを app\views\pages\home.html.erb に設定
  root 'pages#home'

  devise_for :users, 
              path: '', 
              path_names: {sign_up: 'register', sign_in: 'login', edit: 'profile', sign_out: 'logout'},
              controllers: {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations'}

  get 'pages/home'
  get '/dashboard', to: 'users#dashboard'
  get '/users/:id', to: 'users#show'

  post '/users/edit', to: 'users#update'

  resources :gigs do
    member do
      delete :delete_photo
      post :upload_photo
    end
    resources :orders, only: [:create]
  end

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end



「app\views\gigs\show.html.erb」ファイルの記述を更新します。


記述更新 app\views\gigs\show.html.erb
96行目の記述を以下に書き換えます。

<%= form_for([@gig, @gig.orders.new]) do |f| %>
    <%= hidden_field_tag 'pricing_type', p.pricing_type %>
    <%= f.submit "購入する (#{number_to_currency(p.price)})", class: "button is-fullwidth is-danger", data: {confirm: "本当によろしいですか?"} %>
<% end %>



app\views\gigs\show.html.erb

<%= render 'shared/categories' %>

<section class="section">
    <div class="container">
        <div class="columns">

            <!-- 左側 -->
            <div class="column is-two-thirds">
                <div class="columns is-multiline">
                    <!-- 画像カルーセル表示 -->
                    <div class="column is-full">   
                        <div class="card">
                            <div class="card-content">
                                <div class="content">
                                    <p class="title is-4"><%= @gig.title %></p>
                                </div>
                                <hr>
                                <div class="hero-carousel" id="carousel-photo">
                                    <% @gig.photos.each do |photo| %>
                                        <div class="carousel-item has-background image is-16by9">
                                            <%= image_tag url_for(photo), class: "is-background", width: "100%" %>
                                        </div>
                                    <% end %>
                                    <% if @gig.video.present? %>
                                        <div class="video-container">
                                            <iframe src="https://www.youtube.com/embed/<%= @gig.video %>" allowfullscreen></iframe>
                                        </div>
                                    <% end %>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- お仕事の内容 -->
                    <div class="column">
                        <div class="card">
                            <div class="card-content">
                                <article class="media">
                                    <div class="media-content">
                                        <p><strong>このお仕事について</strong></p>
                                        <hr>
                                        <%= @gig.description %>
                                    </div>
                                </article>
                            </div>
                        </div>                        
                    </div>
                    
                </div>
            </div>

            <!-- 右側 -->
            <div class="column">
                <div class="columns is-multiline">

                    <!-- 価格プラン -->
                    <div class="column is-full">
                        <div class="tabs is-fullwidth" id="tabs">
                            <ul>
                                <% Pricing.pricing_types.each do |key, value| %>
                                    <li class="tab <%= 'is-active' if value == 0 %>" data-tab="<%= key %>" style="<%= 'display: none' if @gig.has_single_pricing && value != 0 %>">
                                        <a>
                                            <% if value == 0 %>
                                                ベーシック
                                            <% elsif value == 1 %>
                                                スタンダード
                                            <% else %>
                                                プレミアム
                                            <% end %>
                                        </a>
                                    </li>
                                <% end %>
                            </ul>
                        </div>

                        <div class="tabs-content">
                            <% @gig.pricings.each do |p| %>
                                <div class="tab-content" id="tab-<%= p.pricing_type %>" style="<%= 'display: none' if !p.basic? %>">
                                    <div class="card">
                                        <div class="card-content">
                                            <div class="media">
                                                <div class="media-content">
                                                    <strong><%= p.title %></strong>                                                                                                        
                                                </div>
                                                <div class="media-right">
                                                    <p class="title is-4"><%= number_to_currency(p.price) %></p>
                                                </div>                                                
                                            </div>
                                            <div class="content f-15">
                                                <p class="m-t-30"><%= p.description %></p>
                                                <p class="m-t-30">
                                                    <strong><i class="far fa-clock"></i> 期日: <%= p.delivery_time %></strong>                                                    
                                                </p>
                                            </div>
                                            <% if (!user_signed_in? && @gig.active) || (user_signed_in? && @gig.active && @gig.user_id != current_user.id) %>
                                                <%= form_for([@gig, @gig.orders.new]) do |f| %>
                                                    <%= hidden_field_tag 'pricing_type', p.pricing_type %>
                                                    <%= f.submit "購入する (#{number_to_currency(p.price)})", class: "button is-fullwidth is-danger", data: {confirm: "本当によろしいですか?"} %>
                                                <% end %>                                                
                                            <% else %>
                                                <button class="button is-fullwidth is-danger" disabled>ご利用できません</button>  
                                            <% end %>
                                            
                                        </div>
                                    </div>
                                </div>
                            <% end %>
                        </div>
                    </div>
                    
                    <!-- プロフィール -->
                    <div class="column">
                        <div class="card">
                            <div class="card-content is-horizontal-center is-flex">
                                <figure class="image is-256x256">
                                    <%= image_tag avatar_url(@gig.user), class: "is-rounded" %>
                                </figure>
                            </div>
                            <div class="card-content f-15">
                                <div class="content has-text-centered">
                                    <p class="title is-5"><%= @gig.user.full_name %></p>
                                    <button class="button is-black is-outlined is-fullwidth">メッセージを送る</button>
                                </div>
                                <article class="media">
                                    <div class="media-content">
                                        <i class="fas fa-user m-r-5"></i> アカウント登録日
                                    </div>
                                    <div class="media-right">
                                        <%= I18n.l(@gig.user.created_at, format: :full_date) %>
                                    </div>
                                </article>
                                <article class="media">
                                    <div class="media-content">
                                        <i class="fas fa-map-marker-alt m-r-5"></i> 出身地
                                    </div>
                                    <div class="media-right">
                                        <%= @gig.user.from %>
                                    </div>
                                </article>
                                <article class="media">
                                    <div class="media-content">
                                        <%= @gig.user.about %>
                                    </div>
                                </article>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
            
        </div>
    </div>
</section>

<script>
    BulmaCarousel.attach('#carousel-photo', {
        slidesToScroll: 1,
        slidesToShow: 1
    });
    $(document).ready(function() {
        $('#tabs li').on('click', function() {
            var type = $(this).data('tab');
            $('#tabs li').removeClass('is-active');
            $(this).addClass('is-active');
            $('.tab-content').hide();
            $('#tab-' + type).show();
        }) 
    })
</script>



ブラウザ確認
http://localhost:3000/gigs/1


お仕事を登録したアカウントと別のアカウントでログインして注文してみます。


購入ボタンを押す
購入ボタンを押す


Posticoでオーダーテーブルを確認してみます。

注文成功
注文成功



↓↓クリックして頂けると励みになります。


[25]注文モデル << [ホームに戻る] >> [27]注文の確認