首页 > 解决方案 > Rails:找不到删除方法。改为渲染 get 方法

问题描述

我正在尝试在我的用户面板中添加删除钱包模型的功能。但是由于某种原因,Rails 将其读取为 GET 方法并引发此错误:

没有路线匹配 [GET] "/wallets/1"

我的视图文件:

= link_to 'Remove wallet', wallet_path(@wallet), method: :delete, data: { confirm: 'Are you sure?' }

我的钱包控制器:

class WalletsController < ApplicationController
    def create
        @wallet = current_user.wallets.create(wallet_params)
        if @wallet.save
            redirect_to wallets_index_path
            flash[:notice] = "New wallet has been added successfully"
        else
            render 'home/wallets_new'
            flash[:alert] = "Your wallet could not be added. Please try again"
        end
    end

    def destroy
        @wallet.delete
        redirect_to wallets_index_path, notice: 'Post has been deleted successfully'
    end

路线:

  devise_scope :user do
    authenticated :user do
      root to: 'home#wallets_index', as: :unauthenticated_root
      resources :wallets, only: [:create, :destroy]
    end

    unauthenticated :user do
      root to: 'home#index', as: :authenticated_root
    end
  end

和耙路线:

钱包 POST /wallets(.:format) 钱包#create

钱包删除 /wallets/:id(.:format) 钱包#destroy

我不确定如何处理这个错误,因为我在我的 link_to 助手中明确告诉 Rails 通过 method: :delete 使用 DELETE 方法。我怎样才能解决这个问题?

标签: ruby-on-railsruby

解决方案


你有

<%= javascript_include_tag 'application' %>

在您的 application.html.erb 中?

如果不是,这可能是原因。另请参阅此处


推荐阅读