Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

While it's generally against our design principles, we need to have a controller execute it's #create action on a GET request (it's part of a workflow that involves a series of redirects for an external service). Does anybody know what the intended way to do this is? I don't really want to step outside of the resourceful routing framework provided by the Rails routes. We don't need the #index action that would usually respond to that GET request.

I found (surprisingly) that this works, but I don't know if it's intended, or if we'd be exploiting a bug in Rails and risking breakage later down the line:

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

If that get :create was get :something, we'd have had 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

Bug, or feature?

like image 839
d11wtq Avatar asked Nov 14 '11 07:11

d11wtq


1 Answers

I know this might not be for the latest routing code, but this link has a ton of information about Rails routing. In section 2.3 it covers how the RESTful routes get built.

Rails Routing from the Inside Out

If I was to guess by what I read briefly there. The default REST actions are manually assigned to a specific route. So when you change the create action to use GET it has some sort 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 internal workings of the routing code for Rails. I would assume that it is always possible that this could change at some point in the future.

like image 112
Chase M Gray Avatar answered Sep 18 '22 02:09

Chase M Gray