Ruby on Rails【createアクション前にメソッドがみつからない】

質問内容

Railsで宿泊予約サイトを作成中の者です。
【実現したい流れ】
①ルーム詳細ページからformでパラメータ入力後
②次のページ(確認ページ)へ値を渡す
③登録ボタンでcreateアクションを実行
④Reservationモデルへ登録し予約詳細ページへ遷移する

困っていること

ルーム詳細ページから遷移先へ値を渡すことはできたのですが、
登録する際にto_dateメソッドが存在しないと出る。

デバッグでcreate前にparams[:start_date]が取得できていないことは
確認できたのですがなぜ取得できないのか分からなかったのでご質問させていただきました。
皆様のお知恵をお知恵をおかしいただけると幸いです。

エラーメッセージ

イメージ説明

該当のソースコード

Ruby

# routes.rbRails.application.routes.draw do devise_for :users # トップページのルーティング root 'top#index' resources :users, only:[:show, :edit, :update] do collection do # URLにidをつけない get 'account', action: :show get 'profile', action: :edit end end # ネスト resources :rooms, only:[:index, :new, :create, :show] do resources :reservations, only:[:new, :create, :show] # newアクションのpostを作成 match '/reservations/new', to: 'reservations#new', via: 'post' endend

Ruby

# reservations_controller.rbclass ReservationsController < ApplicationController def new @reservation = Reservation.new @room = Room.find(params[:room_id]) end def create @room = Room.find(params[:room_id]) @reservation = Reservation.new(reservation_params) # room_idの情報はフォームから送られないので、deviseのメソッドで追加 @reservation.room_id = current_user.id if @reservation.save redirect_to root_path else render :new end end def show @reservation = Reservatio n.find(params[:id]) @room = Room.find(params[:room_id]) end private def reservation_params params.require(:reservation).permit(:start_date, :end_date, :people, :total_money).merge(room_id: params[:room_id]) endend

HTML

# views/rooms/show.html.erb <div class='wrapper'> <div class="room-show"> <div class="room-show-inner"> <div class="room-detail"> <div class="room-image"> <%= image_tag @room.image.url %> </div> <div class="room-show-profile"> <%# 後ほど変更(ルーム登録者の画像に変更) %> <%= image_tag 'default.png', size: '80x80', class: 'rounded-circle' %> <div> <h3><%= @room.name %></h3> <p><%= @room.address %></p> </div> </div> <p class='room-show-introduction'><%= @room.introduction %></p> </div> <div class="room-show-reservation"> <div class="reservation-flex"> <p>¥<%= @room.money %>/日</p> <%= form_with url: new_room_reservation_path(@room), local: true do |f| %> <div class="form-group"> <%= f.label :start_date, '開始日' %> <%= f.date_field :start_date, autofocus: true, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :end_date, '終了日' %> <%= f.date_field :end_date, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :people, '宿泊人数' %> <%= f.number_field :people, class: 'form-control' %> </div> <div> <%= f.submit '予約する', class: 'btn btn-primary btn-block text-center' %> </div> <% end %> </div> </div> </div> </div> </div>

HTML

# views/reservations/new.html.erb <div class="wrapper"> <div class="reservation-new"> <div class="reservation-new-inner"> <h2>予約内容確認</h2> <p class='room-money'>¥<%= @room.money %>/日</p> <%= form_with model: [@room, @reservation], url: room_reservations_path, local: true do |f| %> <div class="form-group"> <%= f.label '開始日' %> <%= f.date_field :start_date, class: 'form-control', readonly: true, value: params[:start_date] %> </div> <div class="form-group"> <%= f.label '終了日' %> <%= f.date_field :end_date, class: 'form-control', readonly: true, value: params[:end_date] %> </div> <%# 文字列からdate型に変換後、date型は普通に計算式でOK 有理数が返るからnumeratorメソッド %> <p class='room-day'>使用日数:<%= (params[:end_date].to_date - params[:start_date].to_date).numerator %>日</p> <p class='room-people'>人数:<%= params[:people] %>人</p> <div class="form-group"> <%= f.label '合計金額' %> <%= f.number_field :total_money, class: 'form-control', readonly: true, value: params[:people].to_i * @room.money %> </div> <p class='confirmed'>上記内容でよろしければ「予約確定」を押してください</p> <div class='check'> <%= f.submit '予約を確定', class: 'btn btn-primary btn-block text-center' %> <%= link_to 'ルーム詳細に戻る', account_users_path, class: 'btn btn-secondary' %> </div> <% end %> </div> </div> </div>

コメントを投稿

0 コメント