gpt4 book ai didi

ruby-on-rails - Rails 4.2.7.1 没有路由匹配 [POST] 更新

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:58 25 4
gpt4 key购买 nike

在routes.rb中有

       resources :subjectheadings do
get "delete"
end

它将生成以下路由

       subjectheading_delete GET    /subjectheadings/:subjectheading_id/delete(.:format) subjectheadings#delete
subjectheadings POST /subjectheadings(.:format) subjectheadings#create
new_subjectheading GET /subjectheadings/new(.:format) subjectheadings#new
edit_subjectheading GET /subjectheadings/:id/edit(.:format) subjectheadings#edit
subjectheading PATCH /subjectheadings/:id(.:format) subjectheadings#update
PUT /subjectheadings/:id(.:format) subjectheadings#update
DELETE /subjectheadings/:id(.:format) subjectheadings#destroy

然后在 View 中

       <%= form_for(@subjectheading, :remote => true, :url => (@subjectheading.new_record? ? subjectheadings_path : subjectheading_path(@subjectheading))) do |f| %>

将生成 HTML 代码

       <form class="edit_subjectheading" id="edit_subjectheading_30" action="/subjectheadings/30" accept-charset="UTF-8" data-remote="true" method="post">
<input type="hidden" name="_method" value="patch">
...

以@subjectheading.persisted 的形式?是真的,但是在 update 期间,Rails 尝试使用 POST 而不是 PATCH

       ActionController::RoutingError (No route matches [POST] "/subjectheadings/30")

知道这里出了什么问题吗?

** 更新1 **

这似乎是与嵌套表单相关的冲突(或错误),因为当它们被注释掉时,将使用 PATCH 方法:

 Started PATCH "/subjectheadings/30" for ... 
Processing by SubjectheadingsController#update as JS

有人遇到过这个问题吗?

** 更新2 **

嵌套表单中有一个单选按钮

  <%= f.fields_for :extsubjectheadings do |ff| %>
<%= ff.radio_button :ext_default, ff.object.ext_default, :name => "subjectheading[extsubjectheadings_attributes][][ext_default]", :checked => ff.object.ext_default %>

这生成了 HTML 代码

  <input name="subjectheading[extsubjectheadings_attributes][][ext_default]" type="radio" value="true" checked="checked" id="subjectheading_extsubjectheadings_attributes_1_ext_default_true">

这个单选按钮制造了麻烦,通过顶层表单使用 POST,否则当被注释掉时按预期使用 PATCH。这里有什么问题? id 是否太长?

最佳答案

您不需要手动指定 url。

<%= form_for(@subjectheading) do %>

<% end %>

Rails 会将 url 设置为 subjectheadings_pathsubjectheading_path,具体取决于记录是否持久化。如果这不起作用,那么您很可能在 Controller 或模型中做错了什么。

除非绝对必要,否则不要通过 GET 添加 destroy 路由。因为这违反了 GET 请求不应有副作用的事实。

此外,在命名表和标识符时,请坚持 snake_case 方案。

# inconsistent and does not work with inflection
subjectheading

# good
subject_heading

关于ruby-on-rails - Rails 4.2.7.1 没有路由匹配 [POST] 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40978418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com