【问题标题】:Redirect PATCH and PUT route rails 5 to use POST重定向 PATCH 和 PUT 路由轨道 5 以使用 POST
【发布时间】:2021-07-21 16:30:44
【问题描述】:

我目前正在将正确的路由添加到旧版 Rails 应用程序中,我似乎无法使用 PATCH、PUT 和 DELETE 路由,因为应用程序目前的工作方式仅基于 GET 和 POST 路由。

所以我已经实现了 REST 路由,但目前,我需要重定向 PATCH、PUT 和 DELETE,直到我们可以更改它以使用正确的路由。

这是我在更新时得到的:

这些是自定义字段的路由# custom_fields

resources :custom_fields, except: %i[show destroy] do
  get :disable, on: :member
  
  collection do
    get :list
    get :edit
    get :disable
    get :enable
    get :order_fields
    post :process_order_fields
  end
end
# remap wrong implmentation of paths
get '/custom_fields/edit/:id', to: redirect('/custom_fields/%{id}/edit')

我尝试了以下方法

post '/custom_fields/:id', to: redirect(custom_field_path(id: %{id}))

但没有骰子。

【问题讨论】:

    标签: ruby-on-rails ruby routes ruby-on-rails-5


    【解决方案1】:

    我不明白你的问题。为什么“需要”添加这些重定向?

    如果您希望定义像 post '/custom_fields/:id' 这样的旧版 POST 路由,但在内部让它执行与非旧版 PUT/PATCH 请求相同的操作,那么只需将其定义为:

    resources :custom_fields, except: %i[show destroy] do
      post :update, on: :member
      # ...
    end
    

    此外,将其定义为您尝试的重定向是无效according to the HTTP specification:

    如果收到 302 状态代码以响应 GET 或 HEAD 以外的请求,用户代理不得自动重定向请求,除非用户可以确认,因为这可能会改变请求被执行的条件发布。

    【讨论】:

    猜你喜欢
    • 2018-03-27
    • 1970-01-01
    • 2019-07-18
    • 2015-02-03
    • 2017-03-23
    • 2020-06-28
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多