Fancy routes, but with #create action using GET instead of POST

While this generally contradicts our design principles, we need the controller to perform its #create action on a GET request (this is part of a workflow that includes a series of redirects for an external service). Does anyone know which particular way to do this? I really don't want to go beyond the inventive routing structure provided by Rails routes. We do not need the #index action, which usually responds to this GET request.

I found (surprisingly) that this works, but I don’t know if it was planned, or if we will use the error in Rails and risk breaking later:

 resources :agreements, :except => [:index, :create] do get :create, :on => :collection end 

If this get :create was get :something , we would have a route like

 Helper: something_agreement_path Request: GET /agreements/:agreement_id/something Action: agreements#something 

But Rails actually generates what we want:

 Helper: agreements_path Request: GET /agreements Action: agreements#create 

Error or function?

+6
source share
1 answer

I know that this may not be for the last routing code, but this link contains a ton of Rails routing information. Section 2.3 describes how to create RESTful routes.

Rails Routing from Inside Out

If I were to guess what I read briefly there. By default, REST actions are manually assigned to a specific route. Therefore, when you modify the create action to use GET, it has some kind of record (hash of routes) that it has already created before creating a new one.

So, to answer your question, I assume that this behavior is based on the inner workings of the routing code for Rails. I would suggest that it is always possible that this may change at some point in the future.

+1
source

Source: https://habr.com/ru/post/901418/


All Articles