如何修复 destroy 函数中的 "undefined method" 错误

How to fix "undefined method" error in destroy function

我正在创建一个页面,登录用户可以在其中查看他们即将进行的预订的列表,并在需要时取消预订。

我试过这个作为销毁函数:

def destroy
    @trip = Reservation.find(params[:id])
    @trip.destroy

    flash[:alert] = "This booking has been cancelled."
    redirect_to your_trips_path 
  end  # destroy/ cancel a booking

产生以下错误

undefined method `space_reservation' for #<#<Class:0x00007f8198a191d8>:0x00007f8195341160>
Did you mean?  space_reservations_url

这里是controller.rb

中的相关代码
class ReservationsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_reservation, only: [:approve, :decline, :destroy]

...

 def your_trips
    @today = DateTime.now
    @trips = current_user.reservations.order(start_date: :desc)
  end

 def destroy
    @reservation = Reservation.find(params[:id])
    @reservation.destroy

    flash[:alert] = "This booking has been cancelled."
    redirect_to your_trips_path 
  end  # destroy/ cancel a booking

Routes.rb

 resources :spaces, except: [:edit] do
    member do
      get 'listing'
      delete :delete_image_attachment
      get 'preload'
      get 'preview'
      get 'get_dates'
      get 'get_times'
      put :deactivate
      put :activate
      get 'browse_spaces'
    end
      resources :reservations, only: [:create]
      resources :calendars
  end

  resources :reservations, only: [:approve, :decline, :destroy] do
    member do
      post '/approve' => "reservations#approve"
      post '/decline' => "reservations#decline"
      delete 'destroy'
    end
  end

查看

<div class="panel-body">

        <% @trips.each do |trip| %>
...          
            <div class="col-md-3 text-right trips-index">
              <% if trip.start_date && trip.end_date > @today %>
              <%= link_to 'Cancel', 
                          space_reservation(trip), 
                          method: :delete, 
                          class:"btn btn-danger",
                          data: { confirm: 'Are you sure?' } %> 
              <% end %>
            </div>
          </div>
          <hr/>
        <% end %>
      </div>

我希望在单击取消按钮时删除预订并且不再出现在预订列表中。有没有人发现我在函数中做错了什么?谢谢。

错误消息说 space_reservation 方法未定义。

在您看来,您在这里调用了那个未定义的方法:

<%= link_to 'Cancel', 
  space_reservation(trip), 
  method: :delete, 
  class:"btn btn-danger",
  data: { confirm: 'Are you sure?' } %> 
<% end %>

假设其他一切正常,如果您创建 space_reservation 方法或找到其他方法来解决问题,那么它应该可以正常工作。