【问题标题】:Routing error updating/creating record路由错误更新/创建记录
【发布时间】:2016-07-12 14:40:04
【问题描述】:

我有一个奇怪的路由错误,我似乎无法解决。我正在使用 Rails 4.2.6 并为客户生成脚手架。我可以查看客户列表,但是当我编辑客户时出现错误

No route matches [POST] "/customers/49"

尽管我确实将路线更改为这个,但一切几乎都是脚手架创建的:

  resources :customers do
    resources :comments, only: [:new, :create, :edit, :update]
  end

但我也尝试使用脚手架默认设置并收到相同的错误。

这些是客户的路线:

               customers GET    /customers(.:format)                                customers#index
                         POST   /customers(.:format)                                customers#create
            new_customer GET    /customers/new(.:format)                            customers#new
           edit_customer GET    /customers/:id/edit(.:format)                       customers#edit
                customer GET    /customers/:id(.:format)                            customers#show
                         PATCH  /customers/:id(.:format)                            customers#update
                         PUT    /customers/:id(.:format)                            customers#update
                         DELETE /customers/:id(.:format)                            customers#destroy

“编辑”页面生成的 HTML 显示它是一个发布请求

<form class="edit_customer" id="edit_customer_49" action="/customers/49" accept-charset="UTF-8" method="post">

“新”页面也是如此

<form class="new_customer" id="new_customer" action="/customers" accept-charset="UTF-8" method="post">

在大多数情况下,一切都是脚手架产生的,所以我不确定为什么会出现错误。为什么生成的 HTML 是使用 POST 生成的,我怎样才能让它工作?

感谢您帮助 Rails 新手。

更新 好的,在我的模型中我有

CUSTOMER_TYPE = ["A", "B"]

在我的编辑/新表单中,我有这个:

  <%= f.label :customer_type %>
  <%= f.select :customer_type, Customer::CUSTOMER_TYPE, {include_blank: true}, {index: nil} %>

如果我从表单中删除上述行,编辑和新表单都可以正常工作!?

【问题讨论】:

  • 这是一个 POST 表单,但该方法应该作为 PATCH 通过。您生成的 HTML 是否有类似 &lt;input type="hidden" name="_method" value="patch"&gt; 的行?
  • 是的@GoGoCarl,我确实看到了。 &lt;form class="edit_customer" id="edit_customer_1" action="/customers/1" accept-charset="UTF-8" method="post"&gt;&lt;input name="utf8" type="hidden" value="&amp;#x2713;" /&gt;&lt;input type="hidden" name="_method" value="patch" /&gt;&lt;input type="hidden" name="authenticity_token" value="g26xmf0NV5AVBS5hTK1ZqTz2r/PwbA0OXTnQjKN45951ivyGzQ9wnCspZCQ+4c9LQRwmrYys+cdFpKWHr7eJ8w==" /&gt;
  • 麦克你明白了吗?
  • 我没有@bkunzi01。仍在努力。
  • @mack 好的,所以表单的 HTML 看起来不错。在您的customers_controller 中,您是否声明了一个方法def update 来处理呼叫?另外,新的客户表格是否有效,还是有同样的问题?

标签: ruby-on-rails ruby-on-rails-4 routes


【解决方案1】:

嘿,您列出的路线表明他们期望“更新”的 PATCH 或 PUT 请求,因此您必须将方法更改为“PUT”而不是“POST”才能正常工作。在您的视图中显示表单,我会更详细地显示您要更改的内容。

【讨论】:

    猜你喜欢
    • 2018-02-09
    • 2019-06-28
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多