【问题标题】:Rails simple_form_for update action routing error [POST] with idRails simple_form_for 更新操作路由错误 [POST] with id
【发布时间】:2021-06-20 17:11:55
【问题描述】:

我尝试使用 Rails 6 通过 simple_form_for 更新客户对象

来自show.html.erb 我有<%= render partial: "form", object: @customer, as: 'customer' %>

我的表单示例:_form.html.erb

<%= simple_form_for customer, remote: true, url: customer_path(customer), method: :patch do |f| %>

不知道为什么,Rails 抛出 RoutingError

ActionController::RoutingError (No route matches [POST] "/customers/1"):

我的路线:

resources :customers, only: %i[show create new update]

【问题讨论】:

  • 删除url: customer_path(customer)method: :patch
  • 不,还有 POST
  • 您的newedit 视图是什么样的?
  • 没有new.html.rbedit.html.erb。从动作show 我的表单通过部分_form.html.erb 呈现。我的请求参数有 _method"=&gt;"patch" 但我最终得到 RoutingError [POST]

标签: ruby-on-rails patch put simple-form-for


【解决方案1】:

config.api_only = false允许中间件Rack::MethodOverride

我有config.api_only = true,这是我的问题。

【讨论】:

    【解决方案2】:

    您可以使用部分编辑并删除参数url: customer_path(customer) and method: :patch。以及自动发送请求放置路径的简单表单

    【讨论】:

    • 我尝试将_form.html.erb 更改为_edit.html.erb,但仍然有POST。
    【解决方案3】:

    试试下面的,不需要指定方法,因为 rails 足够聪明,可以识别对象是新的还是持久的。并检查您的路线。

    【讨论】:

    • 不,还有 POST
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多