重定向 PATCH 和 PUT 路由 rails 5 以使用 POST
Redirect PATCH and PUT route rails 5 to use POST
我目前正在将正确的路由添加到遗留 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}))
但没有骰子。
我不明白你的问题。为什么你“需要”添加这些重定向?
如果您希望像 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:
If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
我目前正在将正确的路由添加到遗留 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}))
但没有骰子。
我不明白你的问题。为什么你“需要”添加这些重定向?
如果您希望像 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:
If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.