Rails:编辑表单发送到发布路由而不是补丁 [英] Rails: Edit form sends to post route instead of patch

查看:34
本文介绍了Rails:编辑表单发送到发布路由而不是补丁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我提交我的编辑表单后,它会路由到 [POST] "/users/1/moves/1/inventory_lists/2";而不是我的路线:user_move_inventory_list_path
补丁
/users/:user_id/moves/:move_id/inventory_lists/:id(.:format)
inventory_lists#update

<%= form_for(@list, url: user_move_inventory_list_url(@user, @move), method: :patch) do |f|%><h3>通过更新项目编辑您的列表</h3><p>任何不在您的库存清单中的物品都会产生额外费用</p><% @list.items.each 做 |item|%><%= f.fields_for item, Item.find_by(id: item.id) do |item_attributes|%><div><%= item_attributes.label :name, "Item Name:"%><%= item_attributes.text_field :name, 'name' =>inventory_list[][item][name]";%>

<div><%= item_attributes.label :room, 房间物品属于:"%><%= item_attributes.text_field :room, 'name' =>inventory_list[][item][room]";%>

<div><%= item_attributes.label :weight, "Item Weight:";%><%= item_attributes.number_field :weight, 'name' =>inventory_list[][item][weight]";%>

<br><br><%结束%><%结束%><h3>添加项目</h3><div id="row1";类=行"><%= f.fields_for :item, Item.new do |item_attributes|%><div><%= item_attributes.label :name, "Item Name:";%><%= item_attributes.text_field :name %>

<div><%= item_attributes.label :room, 房间物品属于:"%><%= item_attributes.text_field :room %>

<div><%= item_attributes.label :weight, "Item Weight:";%><%= item_attributes.number_field :weight %>

<%结束%>

<div><输入类型=按钮"id=btnAdd"值=添加另一个项目"/><输入类型=按钮"id=btnDel"值=删除项目"/>

<%= f.submit '更新列表'%>

<%结束%>

解决方案

嘿 - 我在这里看到的一件事是你有 user_move_inventory_list_url 而不是更可取的 user_move_inventory_list_path特别是对于内部用例.这可能是您的问题的根源.

您应该几乎总是使用 _path 变体,除非您提供外部用例的链接,可能是从共享 twitter URL 或您发送的时事通讯 URL 对您网站页面的引用通过邮件等方式

Rails 应该自动包含一个隐藏的 html 字段来覆盖 HTTP 方法.

您可以在这里阅读更多内容:https://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

After I submit my edit form it routes to [POST] "/users/1/moves/1/inventory_lists/2" Instead of my route: user_move_inventory_list_path
PATCH
/users/:user_id/moves/:move_id/inventory_lists/:id(.:format)
inventory_lists#update

<%= form_for(@list, url: user_move_inventory_list_url(@user, @move), method: :patch) do |f| %>
<h3>Edit your list by updating Items</h3>
<p>Any items not in your inventory list will incure additional fees</p>
<% @list.items.each do |item| %>
    <%= f.fields_for item, Item.find_by(id: item.id) do |item_attributes| %>
        <div>
            <%= item_attributes.label :name, "Item Name:"%>
            <%= item_attributes.text_field :name, 'name' => "inventory_list[][item][name]" %>
        </div>
        <div>
            <%= item_attributes.label :room, "Room Item Belongs to:" %>
            <%= item_attributes.text_field :room, 'name' => "inventory_list[][item][room]" %>
        </div>
        <div>
            <%= item_attributes.label :weight, "Item Weight:" %>
            <%= item_attributes.number_field :weight, 'name' => "inventory_list[][item][weight]" %>
        </div>
        <br><br>
    <% end %>
<% end %>

<h3>Add Items</h3>
<div id="row1" class="row">
        <%= f.fields_for :item, Item.new do |item_attributes| %>
                <div>
                    <%= item_attributes.label :name, "Item Name:" %>
                    <%= item_attributes.text_field :name %>
                </div>
                <div>
                    <%= item_attributes.label :room, "Room Item Belongs to:" %>
                    <%= item_attributes.text_field :room %>
                </div>
                <div>
                    <%= item_attributes.label :weight, "Item Weight:" %>
                    <%= item_attributes.number_field :weight %>
                </div>
        <% end %>
</div>
<div>
    <input type="button" id="btnAdd" value="Add Another Item" />
    <input type="button" id="btnDel" value="Remove Item" />
</div>


<%= f.submit 'Update List' %>

<% end %>

解决方案

Hey – one thing I can see here is that you have user_move_inventory_list_url instead of the more advisable user_move_inventory_list_path especially for internal use cases. This is likely where your issue stems from.

You should almost always use the _path variant unless you are providing links for external use cases, maybe a reference to a page on your website from a shared twitter URL, or a newsletter URL you are sending via mail, etc.

Rails should automatically be including a hidden html field to override the HTTP method.

You can read some more here: https://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark

这篇关于Rails:编辑表单发送到发布路由而不是补丁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆