【发布时间】: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 是否有类似
<input type="hidden" name="_method" value="patch">的行? -
是的@GoGoCarl,我确实看到了。
<form class="edit_customer" id="edit_customer_1" action="/customers/1" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="_method" value="patch" /><input type="hidden" name="authenticity_token" value="g26xmf0NV5AVBS5hTK1ZqTz2r/PwbA0OXTnQjKN45951ivyGzQ9wnCspZCQ+4c9LQRwmrYys+cdFpKWHr7eJ8w==" /> -
麦克你明白了吗?
-
我没有@bkunzi01。仍在努力。
-
@mack 好的,所以表单的 HTML 看起来不错。在您的
customers_controller中,您是否声明了一个方法def update来处理呼叫?另外,新的客户表格是否有效,还是有同样的问题?
标签: ruby-on-rails ruby-on-rails-4 routes