Same Rails 4 routes for GET and POST requests

孤人 提交于 2019-12-02 17:16:04

From the match documentation, you can use match as long as you have via:

match "user/account" => "user#account", as: :user_account, via: [:get, :post]

Edit: Added a as: parameter so that it will be accessible via a url helper. user_account_path or user_account_url in this case.

On routes, the match method will no longer act as a catch-all option. You should now specify which HTTP verb to respond to with the option :via

Rails 3.2

match "/users/:id" => "users#show"

Rails 4.0

match "/users/:id" => "users#show", via: :get

or specify multiple verbs

match "/users" => "users#index", via: [:get, :post]

Another option for better Rails 3.2 compatibility is to just specify your actions with explicit get, post, or any other HTTP verb. With this option, you still get your code running today and future proof it for the upgrade.

Rails 3.2 and 4.0 compatible

get "/users/:id" => "users#show"

multiple verbs

get "/users" => "users#index"
post "/users" => "users#index"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!