简体   繁体   English

没有路由匹配POST混乱

[英]No route matches POST confusion

I am very unfamiliar with routing and the entire back-end of Rails in general. 我对路由和整个Rails的后端非常陌生。 I am attempting to have a click on "edit cart" lead to the edit page, I have the edit_cart_path and corresponding view- but when I click the edit cart button, I get 我试图点击“编辑购物车”进入编辑页面,我有edit_cart_path和相应的视图,但是当我单击编辑购物车按钮时,我得到了

Routing Error
No route matches [POST] "/carts/21/edit"

I have resources :carts in routes.rb, I have get "/carts/:id/edit" => "carts#edit" as well. 我在routes.rb中有资源:carts,我也得到了“ / carts /:id / edit” =>“ carts#edit”。 Have tried a couple other methods including "via: get". 尝试了其他几种方法,包括“ via:get”。 Why is it insisting on POST, and how to solve this? 为什么坚持POST,如何解决呢?

I'm guessing you're doing something like this, in your view: 我认为您认为您正在执行以下操作:

button_to(edit_cart_path(@cart))

When using a button_to helper, the default HTTP method will be POST. 使用button_to帮助程序时,默认的HTTP方法为POST。

You'll have to do explicitly define the HTTP method you want to execute: 您必须明确定义要执行的HTTP方法:

button_to(edit_cart_path(@cart), method: :get)

I would encourage you to use the link_to helper instead, and add any button effect using CSS: 我鼓励您改用link_to帮助器,并使用CSS添加任何按钮效果:

link_to(edit_cart_path(@cart), class: 'btn')

From the Rails 4 documentation: 从Rails 4文档中:

button_to(name, options = {}, html_options = {}) button_to(名称,选项= {},html_options = {})

The options hash accepts the same options as url_for. options哈希接受与url_for相同的选项。

There are a few special html_options: 有一些特殊的html_options:

:method - Symbol of HTTP verb. :method-HTTP动词的符号 Supported verbs are :post, :get, :delete and :put. 支持的动词是:post,:get,:delete和:put。 By default it will be :post. 默认情况下,它将是:post。

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

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