문제

I'm still pretty new to RoR and have run into a problem I can't seem to solve. I've already built the functions to add new products to the database and allow a user to add individual items to their cart, my delete method just gives me an error saying it can't find the delete method in the carts_controller. Any help would be appreciated.

\app\views\cart\show.html.erb

<%= button_to 'Empty Cart', @cart, method: :delete,
    data: { confirm: 'are you sure?'} %>

\app\controllers\carts_controller.rb

def destroy
 @cart.destroy if @cart.id == session[:cart_id]
 session[:cart_id] = nil
 respond_to do |format|
   format.html { redirect_to store_url,
     notice: 'Your cart is empty' }
  format.json { head :no_content }
  end
end

private
def set_cart
  @cart = Cart.find(params[:id])
end

Error Message Given

Unknown Action
The action 'destroy' could not be found for CartsController

\config\routes.rb

Depot::Application.routes.draw do

root 'store#index', as: 'store'
resources :line_items
resources :carts
get "store/index"
resources :products

end
도움이 되었습니까?

해결책

Depends on what is currently set in your @cart variable, can you put out an inspect for that?

Otherwise, you can also try something like this:

<%= link_to 'Empty Cart', cart_path(@cart), method: :delete, confirm: 'are you sure?' %>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top