FLATPICKR 提交? - 尝试制作预订按钮但不起作用

问题描述 投票:0回答:0

事情,我尝试了很多东西根本不起作用,当我从我的表格发出我的帖子请求时,不知何故我的终端出现了这种错误:

20:21:01 web.1  | Started POST "/bookings" for ::1 at 2023-02-19 20:21:01 +0100
20:21:01 web.1  | Processing by BookingsController#create as TURBO_STREAM
20:21:01 web.1  |   Parameters: {"authenticity_token"=>"[FILTERED]", "booking"=>{"start_time"=>"2023-02-28", "end_time"=>"2023-03-08"}, "commit"=>"Schedule your appointment"}
20:21:01 web.1  |   User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 3], ["LIMIT", 1]]
20:21:01 web.1  | Completed 404 Not Found in 4ms (ActiveRecord: 0.9ms | Allocations: 2628)
20:21:01 web.1  | 
20:21:01 web.1  | 
20:21:01 web.1  |  
20:21:01 web.1  | ActiveRecord::RecordNotFound (Couldn't find Game without an ID):
    20:21:01 web.1  |   
    20:21:01 web.1  | app/controllers/bookings_controller.rb:38:in `set_game'
    20:21:01 web.1  | Started GET "/games/37" for ::1 at 2023-02-19 20:21:01 +0100
    20:21:01 web.1  | Processing by GamesController#show as HTML

所以我有一个使用该设计创建的 Games_controller 用户和一个 Bookings_controller。在我的显示页面中,我看到了我的预订选项,但不知何故,当我点击提交按钮时,没有任何反应,我得到了我之前分享的错误,我不确定为什么。我不明白的是,为什么我没有任何类型的标志,比如我的预订根本没有被保存,或者如果它保存了,我应该有某种标志。所以经过深思熟虑,我决定在这里问。

因为我以 _show.html.erb 的形式定义了我的 games_id。因此,如果有人对此有解决方案,那就太好了

GAMES_CONTROLLER 
class GamesController < ApplicationController   def home
    authorize Game   end

  def index
    @games = Game.all
    @games = policy_scope(Game)   end

  def new
    @game = Game.new
    authorize @game   end

  def create
    @game = Game.new(game_params)
    @game.user = current_user
    if @game.save
      redirect_to game_path(@game) # later to be changed in game_path(@game) when we have a show
    else
      render :new, status: :unprocessable_entity
    end
    authorize @game   end

  def show
    @game = Game.find(params[:id])
    @booking = Booking.new(game: @game)
    authorize @game   end

  def edit
    @game = Game.find(params[:id])
    authorize @game   end

  def update
    @game = Game.find(params[:id])
    if @game.update(game_params)
      redirect_to game_path(@game)
    else
      render :new, status: :unprocessable_entity
    end
    authorize @game   end

  def destroy
    @game = Game.find(params[:id])
    @game.destroy
    redirect_to games_path
    authorize @game   end

  private

  def game_params
    params.require(:game).permit(:price, :title, :description, :photo)   end end


BOOKING_CONTROLLER 
class BookingsController < ApplicationController   before_action :set_game, only: [:create]   before_action :set_booking, only: [:show]

  def index
    authorize Booking
    @bookings = policy_scope(Booking)
    puts "Number of bookings: #{@bookings.count}"   end

  def show
    authorize @booking   end

  def create
    puts "Params: #{params.inspect}"
    @booking = Booking.new(booking_params.merge(game: @game, user: current_user))
    authorize @booking

    if @booking.save
      flash[:notice] = "Booking was successfully created."
      redirect_to @booking
    else
      flash[:alert] = "There was an error creating the booking."
      redirect_to @game
    end

    Rails.logger.debug "Params: #{params.inspect}"
    Rails.logger.debug "Game: #{@game.inspect}"
    Rails.logger.debug "Booking: #{@booking.inspect}"
    Rails.logger.debug "Booking params: #{booking_params.inspect}"
    Rails.logger.debug "Current user: #{current_user.inspect}"   end

  private

  def set_game
    @game = Game.find(params[:game_id])
    puts @game.inspect   end

  def set_booking
    @booking = Booking.find(params[:id])   end

  def booking_params
    params.require(:booking).permit(:start_date, :end_date, :game_id)   end

  def set_dates
    self.start_date = start_date.to_date
    self.end_date = end_date.to_date   end

end


GAMES - SHOW.HTML.ERB 
<%= render 'shared/navbar' %>

<h1>Show</h1> <h2><%= @game.title %></h2> <%= cl_image_tag @game.photo.key,
      width: 400, height: 300, crop: :fill %> <p><%= @game.description %></p> <p><%= @game.price %></p> <p><% @game.categories.each do |category| %>   <%= category.name %> <% end %> </p>

<p><%= link_to 'Edit game listing', edit_game_path(@game) if policy(@game).edit?%></p> <p><%= link_to 'Delete game listing', game_path(@game), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" }if policy(@game).edit?%></p> <p><%= link_to 'Back to games', games_path %></p>

<h1>BOOKINGS</h1>

<%= render partial: 'bookings/show', locals: { booking: @booking } %>


BOOKING _SHOW.HTML.ERB 
<%= simple_form_for @booking, data: { controller: 'flatpickr' } do |f| %>   <%= f.hidden_field :game_id, value: @game.id %>   <%= f.input :start_time, as: :string, input_html: { data: { flatpickr_target: "startTime" } } %>   <%= f.input :end_time, as: :string, input_html: { data: { flatpickr_target: "endTime" } } %>   <%= f.submit 'RENT THE GAME', class: 'btn btn-primary' %> <% end %>

<h1>Booking Details</h1> <p><strong>Start date:</strong> <%= @booking.start_date %></p> <p><strong>End date:</strong> <%= @booking.end_date %></p>

<h1>Here Come the calendar</h1> <%= render partial: 'bookings/calendar', locals: { bookings: @bookings } %>


BOOKING _CALENDAR.HTML.ERB 
<div id='calendar'></div> <% if @bookings.present? %>   <% @bookings.each do |booking| %>
    <%= booking.inspect %><br> # Add this line   <% end %> <% else %>   No bookings to display. <% end %>

<script>   document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
      initialView: 'dayGridMonth',
      events: [
        <% if @bookings %>
          <% @bookings.each do |booking| %>
            {
              title: '<%= booking.game.title %>',
              start: '<%= booking.start_date %>',
              end: '<%= booking.end_date %>',
              color: 'red'
            },
          <% end %>
        <% end %>
      ]
    });
    calendar.render();   }); </script>

<html lang='en'>   <head>
    <meta charset='utf-8' />
    <script src='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.js'></script>
    <script>

      document.addEventListener('DOMContentLoaded', function() {
        var calendarEl = document.getElementById('calendar');
        var calendar = new FullCalendar.Calendar(calendarEl, {
          initialView: 'dayGridMonth'
        });
        calendar.render();
      });

    </script>   </head>   <body>
    <div id='calendar'></div>   </body> </html>
ruby-on-rails forms submit flatpickr
© www.soinside.com 2019 - 2024. All rights reserved.