予約確認ページに遷移できない
解決したいこと
入力フォームから予約確認ページに遷移できるようにしたい
例)
Ruby on Railsで宿予約アプリを作成しています。
エラー等はないのですが、「予約」ボタンを押しても入力フォームから遷移できません。
閲覧ページ/入力フォーム(rooms/show)
予約確認ページ(reservation/new)
該当するソースコード
room/show.html.erb
<%= form_with model: @reservation, method: :post, url: new_reservation_path do |f| %>
<%= f.hidden_field :room_id, value: @room.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<h5>
¥<%= @room.price %>/日
</h5>
<%= f.label :start_day, "開始日" %><br>
<%= f.date_field :start_day, min: Date.current, required: true %>
<%= f.label :end_day, "終了日" %><br>
<%= f.date_field :end_day, min: Date.current.next_day, required: true %>
<%= f.label :people, "人数" %><br>
<%= f.number_field :people, required: true, min: 1 %>
<% if user_signed_in? %>
<%= f.submit "予約する", class:"" %>
<% else %>
<%= link_to "ログイン", new_user_session_path %>
<% end %>
<% end %>
reservation/new.html.erb
<table>
<h2>予約内容確認</h2>
<tr>
<h3>
¥<%= @room.price %>/日
</h3>
</tr>
<tr>
<td>開始日</td>
<td><%= @reservation.start_day %></td>
</tr>
<tr>
<td>終了日</td>
<td><%= @reservation.end_day %></td>
</tr>
<tr>
<td>使用日数</td>
<td><%= @days %></td>
</tr>
<tr>
<td>人数</td>
<td><%= @reservation.people %>人</td>
</tr>
<tr>
<td>合計金額</td>
<td>
<%= @price %>
</td>
</tr>
<tr>
<%= form_with model: @reservation do |f| %>
<%= f.hidden_field :room_id, value: @room.id %>
<%= f.hidden_field :people %>
<%= f.hidden_field :start_day %>
<%= f.hidden_field :end_day %>
<td><%= f.submit "予約を確定する" %></td>
<% end %>
</tr>
<tr>
<%= link_to "ルーム詳細に戻る", room_path(@room) %>
</tr>
</table>
ルーティング
post "reservations/new" ,to: "reservations#new"
roomsコントローラ
def show
@room = Room.find(params[:id])
@reservation = Reservation.new
end
reservationsコントローラ
def new
@room = Room.find(params[:reservation][:room_id])
@reservation = Reservation.new(reservation_params)
@price = @room.price * @reservation.people * (@reservation.end_day - @reservation.start_day).to_i
@days = (@reservation.end_day - @reservation.start_day).to_i
end
private
def reservation_params
params.require(:reservation).permit(:start_day, :end_day, :people, :room_id, :user_id)
end
また、コンソール上では以下のような表記が出るのですが、調べてもわかりませんでした。
Started POST "/reservations/new" for ::1 at 2022-07-20 22:50:30 +0900
Processing by ReservationsController#new as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "reservation"=>{"room_id"=>"2", "user_id"=>"1", "start_day"=>"2022-07-21", "end_day"=>"2022-07-22", "people"=>"1"}, "commit"=>"予約する"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Room Load (0.1ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/reservations_controller.rb:7:in `new'
Rendering layout layouts/application.html.erb
Rendering reservations/new.html.erb within layouts/application
Rendered reservations/new.html.erb within layouts/application (Duration: 2.0ms | Allocations: 1078)
Rendered layout layouts/application.html.erb (Duration: 6.1ms | Allocations: 4617)
Completed 200 OK in 13ms (Views: 7.6ms | ActiveRecord: 0.2ms | Allocations: 7391)
恐れ入りますがご助言いただけますと幸いです。
0