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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With