【问题标题】:ROR: Trying to trigger update route using 'put' but for some reason deleting insteadROR:尝试使用“put”触发更新路由,但由于某种原因删除而不是
【发布时间】:2020-06-01 15:38:50
【问题描述】:

我正在开发一个 Rails 应用程序,我正在使用 devise 进行身份验证。我遇到了一个问题,当我尝试更新用户详细信息而不是更新用户时,用户刚刚被删除。

在registrations/edit.html.erb 我有:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), method: :put) do |f| %>

  ... input tags and styling

<% end %>

生成html:

<form class="edit_user" id="edit_user" enctype="multipart/form-data" action="/users" accept-charset="UTF-8" method="post">
<input type="hidden" name="_method" value="put" />
<input type="hidden" name="authenticity_token" value="Q2U4gEBfaIwCo2Vwi2fwXgwYLjzE4HpoDC8KN52m2GGqEdb94jd/c3TyFhEJtQEziHat9zFQs+e+fRJp4/j2WA==" />

... rest of the input tags

</form>

这里的关键是 rails 中的 &lt;input type="hidden" name="_method" value="put" /&gt; 这应该会触发 PUT 路由,但由于某种原因,当我提交表单时它会触发删除路由:

Started DELETE "/users" for 127.0.0.1 at 2020-06-01 16:15:48 +0100
   (0.6ms)  SET NAMES utf8mb4 COLLATE utf8mb4_bin,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
   (0.2ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC

以前有没有人遇到过这种情况并知道是什么原因造成的?

编辑

我的配置/routes.rb

# frozen_string_literal: true

Rails.application.routes.draw do
  resources :posts
  namespace :admin do
      resources :users

      root to: "users#index"
    end
  devise_for :users, module: 'users'
  get 'profile', to: 'profile#index'
  root to: "home#index"
end

【问题讨论】:

  • 分享您的 routes.rb 文件或在控制台上点击“rake routes”命令并分享结果。
  • 当然,已添加到帖子中:)

标签: ruby-on-rails ruby devise


【解决方案1】:

原来我只是在傻。在输入字段中,有一个 ERB button_to 帮助程序用于取消帐户(或者更确切地说是删除用户),该帮助程序在更新表单中呈现一个表单,而该内部表单是在点击外部提交按钮时提交的内容表单,导致触发删除动作

编辑:要明确避免这种情况的方法是确保 button_to 帮助器位于您要提交的表单之外或在单独的页面上。

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 2017-08-17
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多