加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用form_tag提交params来更新方法会给出未定

发布时间:2020-12-17 02:33:35 所属栏目:百科 来源:网络整理
导读:我有一个这样的表格: = form_tag item_path(@item) do # some inputs 这给出了以下HTML: form method="post" action="/items/1" accept-charset="UTF-8" !-- some inputs --/form 当我提交它时 – 我收到以下错误: No route matches [POST] "/items/1" 但
我有一个这样的表格:

= form_tag item_path(@item) do
  # some inputs

这给出了以下HTML:

<form method="post" action="/items/1" accept-charset="UTF-8">
  <!-- some inputs -->
</form>

当我提交它时 – 我收到以下错误:

No route matches [POST] "/items/1"

但是,当我将SimpleForm用于同一项时 – HTML几乎相同并且它可以工作:

# code:
= simple_form_for @item do |f|
  ...
# output:
<form id="edit_item_1" novalidate="novalidate" method="post" action="/items/1" accept-charset="UTF-8">
...

你看到我的代码中有任何错误吗?

如何确保我的form_tag向项目更新方法提交params?

解决方法

我不确定为什么simple_form_for有效.但通常Rails form_tag方法使用http方法POST生成表单.

<form method="post"

所以在你的情况下,如果你想提交表格来更新方法,那么http方法应该是PUT.您可以在rails form_tag中显式指定http方法.

form_tag item_path(@item),:method => :put do |f|

通常人们总是准备form_for而不是form_tag,他们将重复使用表单模板进行创建和更新.因为form_for将根据记录状态设置正确的http方法.如果记录是新的,它会将http方法设置为POST else PUT.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读