46

In Rails 3 Match used to point to an action for both "GET" and "POST" and other type of requests.

match "user/account" => user#account

Now this will point to account action of user's controller for both GET and POST requests. As in Rails 4 "match" has been deprecated, can we create same route for GET and POST in Rails 4?

1
  • 2
    match won't just match GET and POST... it will match PUT, PATCH and DELETE as well. Sep 13, 2013 at 7:59

2 Answers 2

95

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.

1
  • 4
    We also need to put " :as => action_name " with route to work with all cases.
    – Adnan Ali
    Sep 13, 2013 at 8:04
46

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"
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.