简体   繁体   English

路由错误没有路由匹配[GET]“/products/1”

[英]Routing Error No route matches [GET] "/products/1"

I'm practicing with RoR with a CRUD, but when I want delete some product, I got this error (like in the image), I don't know why got this error, someone can help me pleas?我正在使用 CRUD 练习 RoR,但是当我想删除某些产品时,出现此错误(如图所示),我不知道为什么会出现此错误,有人可以帮我吗?

This is my routes.rb这是我的路线.rb

Rails.application.routes.draw do
  
  get '/products', to: 'products#index'
  get '/products/new', to: 'products#new'
  post '/products', to: 'products#create'
  post '/products/:id', to: 'products#shows', as: 'product'
  get '/products/:id/edit', to: 'products#edit', as: 'edit_product'
  patch '/products/:id', to: 'products#update'
  delete '/products/:id', to: 'products#delete'
  # resources :products    
end

This is my products_controller.rb这是我的 products_controller.rb

class ProductsController < ApplicationController
  
  def delete
    @product = Product.find(params[:id])
    @product.destroy
    redirect_to products_path
  end

  private
    def product_params
      params.require(:product).permit(:name, :description, :price)
    end
  
end

And this is my index.html.rb, where I'm listing the products.这是我的 index.html.rb,我在其中列出了产品。

<h1>Products list</h1>
<table>
  <tr>
    <thead>
      <th>Name</th>
      <th>Description</th>
      <th>price %></th>
    </thead>
  </tr>
  <% @products.each do |product| %>
    <tr>
      <td><%= product.name %></td>
      <td><%= product.description %></td>
      <td><%= product.price %></td>
      <td>
        <%= link_to "Update", edit_product_path(product) %>
        <%= link_to "Delete", product, method: :delete, data: { confirm: 'Are you sure?' } %>
      </td>
    </tr>
  <% end %>
</table>

Looks like you are using Rails 7 since method: :delete is no longer working without ujs .看起来您正在使用 Rails 7,因为method: :delete在没有ujs的情况下不再工作。

So, it leads to GET request and there is really no such route in your config.因此,它会导致 GET 请求,并且您的配置中实际上没有这样的路由。 This issue was discussed in this question and the solution is:在这个问题中讨论了这个问题,解决方案是:

  <%= link_to "Delete", product, data: {  turbo-method: :delete, confirm: 'Are you sure?' } %>

But there is a catch: looks like not all versions of Rails 7 support this behavior.但是有一个问题:看起来并非所有版本的 Rails 7 都支持这种行为。 And this will not work inside the form because actual mechanics of changing the method is based on creating a hidden form.这在表单内部不起作用,因为更改方法的实际机制是基于创建隐藏表单。 There is a discussion in the issue and a hint in the docs : you should use links only for GET requests.问题中有讨论, 文档中有提示:您应该仅将链接用于GET请求。 For other requests use actual forms or buttons, so button_to will do the job better since it can change method and use confirmation prompt.对于其他请求,请使用实际的 forms 或按钮,因此button_to会做得更好,因为它可以更改方法并使用确认提示。

I infer that you try to do the show action.我推断你尝试做show动作。 If it is the case, the HTML verb is wrong.如果是这种情况,则 HTML 动词是错误的。 You are using post instead of get .您正在使用post而不是get

The POST verb is most often utilized to create new resources. POST动词最常用于创建新资源。

The HTTP GET method is used to read (or retrieve) a representation of a resource HTTP GET方法用于读取(或检索)资源的表示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM