How to fix "undefined method" error in destroy function

Erin B.

I am creating a page where a logged in user can view a list of their upcoming reservations and cancel a reservation if need be.

I've tried this as the 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

which produces the following error

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

Here is the relevant code in the 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

View

<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>

I would like for the reservation to be deleted when the cancel button is clicked and to no longer appear in the list of reservations. Does anyone spot what I did wrong in the function? Thanks.

Justin Hendrickson

The error message says that the space_reservation method is undefined.

In your view, you called that undefined method here:

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

Assuming everything else works, if you either create the space_reservation method or find another way to resolve the issue, then it should work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix 'Uncaught Error: Call to undefined method' in PHP

How to fix 'undefined method error' when method exists?

Call to undefined method; how to fix?

How to fix in KnexJs the error destroy of undefined

How to fix "undefined method `empty?' for ...:Float"

How to fix "undefined method `empty?' for #<ContactInformation:0x007fc8be45cc98>: error?

How to fix this error "undefined method `encoding' for nil:NilClassa " and get canceling subscription plan worked?

Undefined method error for input parameters to a function

How to fix this (is not a function) error?

How to fix "... is not a function" error?

How to catch "error":"undefined method `[]' for nil:NilClass"

How to debug undefined method form_with error

How can i solve an undefined method error

how to fix the following error : undefined method `PUT' for #<ActionDispatch::Routing:: Mapper:0x3f8ee48> in rails 3.2?

How to fix Call to undefined method Maatwebsite\Excel\Excel::create()?

How to fix undefined method `new' for "Rack::Cors":String

How to fix "NoMethodError (undefined method []' for nil:NilClass): app/controllers/..."

How to fix 'undefined method' enum in rspec test with FactoryBot?

How to Fix "Attempted to call an undefined method named "createNamedBuilder..."?

How to fix undefined method in nested models? post/create in Rails app

How to fix this function? Results in Error

How to fix 'callback is not a function error'?

How to fix "dispatch is not a function" error

ExponentialBackoff job Reload function Error: Call to undefined method

undefined method error when pass function from one class to the other

Error "Call to undefined method" when attempting hasFile() function

how to fix firebase function Unhandled error Error

How to destroy the gridrotator() function?

Best way to fix undefined method