Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different controller actions for POST and GET requests at the same route in Rails

I want to route the same address, e.g., 'http://server/path' to different controller actions depending on the request type, whether it is a GET or POST request.

How can I do that in Rails?

Thanks!

like image 736
Carlos Melo Avatar asked May 10 '11 13:05

Carlos Melo


People also ask

What decides which controller receives which requests in Rails?

Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action's purpose is to collect information to provide it to a view.

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.


2 Answers

get "/path" => "controller#get_action"
post "/path" => "controller#post_action"
like image 190
iceydee Avatar answered Sep 27 '22 21:09

iceydee


I think you could do this:

match '/path' => 'controller#action', :via => :get
match '/path' => 'controller#another_action', :via => :post
like image 39
bassneck Avatar answered Sep 27 '22 21:09

bassneck