james
james

Reputation: 4047

On re-submit of form with errors, a PATCH request is called when it's supposed to be a POST

Something very funny is happening... my controller has two checks that are made separately before it allows the form to be submitted (the code below is simplistic, there are other things happening). If either checkpoint fails, the new page is re-rendered.

Here's the code:

def create
    if checkpointone == fail
        render 'new'
    else
        if checkpointtwo == fail
            render 'new'
        else
            redirect_to action: 'success'
        end
    end
end

Here's the funny flow problem I'm running into:

  1. user enters data that fails checkpointone and passes checkpointtwo
  2. user submits
  3. new page successfully re-rendered with original parameters and error message
  4. user still enters incorrect data (either checkpointone fails again, or it passes but checkpointtwo fails)
  5. user submits
  6. application fails with an error No route matches [PATCH] "/requests"

But why on earth is it looking for a PATCH all of the sudden? Who told it to? The weird thing is that if I start with a fail in checkpointtwo and a pass in checkpointone, from then on, I can make any number of fails and resubmissions in various combinations, and I'll always get the correct action: new page re-rendered with original parameters and error message.

Snippet of view code:

<%= form_for @requestrecord, :url => { action: 'create' }, :html=> {:id => 'form'} do |f| %>

Snippet of routes code:

get 'requests/new', to: 'requests#new', as: 'new_request'
post 'requests', to: 'requests#create'

Upvotes: 1

Views: 54

Answers (1)

San
San

Reputation: 1954

In Rails 4, PATCH is used for updates. Could it be that your record is being created even when a checkpoint fails. This would cause Rails to think that the subsequent request is an update request, rather than create request.

Upvotes: 1

Related Questions