ルートの設定をします。
記述追加 config\routes.rb(14,15行目)
get '/selling_orders', to: 'orders#selling_orders' get '/buying_orders', to: 'orders#buying_orders'
記述追加 config\routes.rb(19行目)
put '/orders/:id/complete', to: 'orders#complete', as: 'complete_order'
config\routes.rb
14,15行目、19行目に記述追加しています。
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' get '/selling_orders', to: 'orders#selling_orders' get '/buying_orders', to: 'orders#buying_orders' post '/users/edit', to: 'users#update' put '/orders/:id/complete', to: 'orders#complete', as: 'complete_order' 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\shared\_navbar.html.erb
73行目と79行目に以下のリンクを追加しています。
- 73行目:「<%= link_to '売れた注文の確認', selling_orders_path, class: "navbar-item" %>」
- 79行目:「<%= link_to '買った注文の確認', buying_orders_path, class: "navbar-item" %>」
<nav class="navbar is-danger" role="navigation" aria-label="main navigation"> <div class="navbar-brand"> <a class="navbar-item" href="/"> <h1>テストサイトOshigoto</h1> </a> <a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample"> <span aria-hidden="true"></span> <span aria-hidden="true"></span> <span aria-hidden="true"></span> </a> </div> <div id="navbarBasicExample" class="navbar-menu"> <div class="navbar-start"> <div class="navbar-item"> <div class="field has-addons"> <div class="control"> <input class="input" type="text" placeholder="どんなお仕事を?"> </div> <div class="control"> <a class="button is-success">検索</a> </div> </div> </div> </div> <div class="navbar-end"> <a class="navbar-item"></a> <a class="navbar-item"></a> <!-- もしログインしていなかったら--> <% if (!user_signed_in?) %> <div class="navbar-item"> <div class="buttons"> <%= link_to "新規ユーザ登録", new_user_registration_path, class: "button is-primary" %> <%= link_to "ログイン", new_user_session_path, class: "button is-light" %> </div> </div> <!-- ログインしていたら --> <% else %> <div class="navbar-item has-dropdown is-hoverable"> <a class="navbar-item" style="margin-right: 50px;"> <figure class="image is-48x48 m-r-5"> <div style="margin-top: 0.6rem;"> <%= image_tag avatar_url(current_user), class: "is-rounded" %> </div> </figure> <%= current_user.full_name %> </a> <div class="navbar-dropdown"> <%= link_to 'ダッシュボード', dashboard_path, class: "navbar-item" %> <%= link_to "ユーザ登録情報編集", edit_user_registration_path, class: "navbar-item" %> <hr class="navbar-divider"> <%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: "navbar-item" %> </div> </div> <% end %> </div> </div> </nav> <% if (user_signed_in?) && !current_page?(root_path) && !current_page?("/gigs/#{params[:id]}") && !current_page?("/users/#{params[:id]}") %> <nav class="navbar has-shadow" style="z-index: 5;"> <div class="container"> <div class="navbar"> <%= link_to 'ダッシュボード', dashboard_path, class: "navbar-item" %> <div class="navbar-item has-dropdown is-hoverable"> <a class="navbar-link">お仕事を売る人</a> <div class="navbar-dropdown"> <%= link_to 'お仕事を登録する', new_gig_path, class: "navbar-item" %> <%= link_to '売れた注文の確認', selling_orders_path, class: "navbar-item" %> </div> </div> <div class="navbar-item has-dropdown is-hoverable"> <a class="navbar-link">お仕事を買う人</a> <div class="navbar-dropdown"> <%= link_to '買った注文の確認', buying_orders_path, class: "navbar-item" %> <a class="navbar-item"></a> </div> </div> </div> </div> </nav> <% end %> <script> $(document).ready(function() { // Check for click events on the navbar burger icon $(".navbar-burger").click(function() { // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu" $(".navbar-burger").toggleClass("is-active"); $(".navbar-menu").toggleClass("is-active"); }); }); </script>
「app\controllers\orders_controller.rb」ファイルに以下の記述を追加します。
記述追加 app\controllers\orders_controller.rb(24行目)
def complete @order = Order.find(params[:id]) if !@order.completed? if @order.completed! flash[:notice] = "保存しました" else flash[:notice] = "問題が発生しました" end redirect_to request.referrer end end
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 def complete @order = Order.find(params[:id]) if !@order.completed? if @order.completed! flash[:notice] = "保存しました" else flash[:notice] = "問題が発生しました" end redirect_to request.referrer end 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
「app\views」フォルダに「orders」フォルダを新規作成して下さい。
作成した「orders」フォルダに「buying_orders.html.erb」ファイルと「selling_orders.html.erb」ファイルを新規作成します。
app\views\orders\buying_orders.html.erb(新規作成したファイル)
<section class="section"> <div class="container"> <p class="title">買った注文の確認</p> <table class="table is-fullwidth"> <thead> <tr> <th>注文日</th> <th>売り主</th> <th>タイトル</th> <th>期日</th> <th>価格</th> <th>ステータス</th> <th>アクション</th> </tr> </thead> <tbody> <% if @orders.blank? %> <tr> <td colspan="7" class="has-text-centered"><h1>表示できる注文がありません。</h1></td> </tr> <% end %> <% @orders.each do |o| %> <tr> <td><%= I18n.l(o.created_at, format: :full_date) %></td> <td><%= o.seller_name %></td> <td><%= link_to o.title, gig_path(o.gig) %></td> <td><%= I18n.l(o.due_date) %></td> <td><%= number_to_currency(o.amount) %></td> <td> <span class="tag <%= 'is-warning' if o.inprogress? %> <%= 'is-success' if o.completed? %>"> <% if o.inprogress? %> 進行中 <% else %> お仕事完了 <% end %> </span> </td> <td> <%= link_to 'お仕事完了にする', complete_order_path(o), method: :put, class: "button is-small is-primary #{'is-hidden' if o.completed?}" %> </td> </tr> <% end %> </tbody> </table> </div> </section>
app\views\orders\selling_orders.html.erb(新規作成したファイル)
<section class="section"> <div class="container"> <p class="title">売った注文の確認</p> <table class="table is-fullwidth"> <thead> <tr> <th>注文日</th> <th>買い主</th> <th>タイトル</th> <th>期日</th> <th>価格</th> <th>ステータス</th> </tr> </thead> <tbody> <% if @orders.blank? %> <tr> <td colspan="6" class="has-text-centered"><h1>表示できる発注はありません。</h1></td> </tr> <% end %> <% @orders.each do |o| %> <tr> <td><%= I18n.l(o.created_at, format: :full_date) %></td> <td><%= o.buyer_name %></td> <td><%= link_to o.title, gig_path(o.gig) %></td> <td><%= I18n.l(o.due_date) %></td> <td><%= number_to_currency(o.amount) %></td> <td> <span class="tag <%= 'is-warning' if o.inprogress? %> <%= 'is-success' if o.completed? %>"> <% if o.inprogress? %> 進行中 <% else %> お仕事完了 <% end %> </span> </td> </tr> <% end %> </tbody> </table> </div> </section>
ブラウザ確認
http://localhost:3000/buying_orders
買ったお仕事が終わったら「お仕事完了」にできます。
http://localhost:3000/selling_orders