Mike N.
Mike N.

Reputation: 89

POST Routing Issue for Custom Form

I have added a custom route, custom controller code, and actioned the custom route in the form, but I get an error regarding the route, even as it shows the route in error message.

The form/view is as follows: views/survey_request/confirmation.html.erb

<%= form_for @survey_request, url: survey_requests_confirm_path do |f| %>
  <p>Your Email Address: <%= f.text_field :customer_email %></p>
  <%= f.hidden_field :survey_token, value: @survey_request.survey_token %>
  <p>Your survey token: <%= @survey_request.survey_token %></p>
  <p><%= f.submit %></p>
<% end %>

My relevant routes in the routes.rb file are as follows:

get 'survey/:id', to: 'survey_requests#confirmation'
put 'survey_requests/confirmation', to: 'survey_requests#confirmation'
put 'survey_requests/confirm', to: 'survey_requests#confirm'

In the survey_requests_controller.rb I have a method defined

def confirm
  #code here to confirm the users email and token
end

When I run the app, the confirmation.html.erb form shows up fine, to include the token passed to it. When I submit the form I get the following error:

No route matches [POST] "/survey_requests/confirm"

However when I scroll down on the same error page, it shows the route:

survey_requests_confirm_path    PUT /survey_requests/confirm(.:format)  survey_requests#confirm

Any suggestions? Thanks!

Upvotes: 0

Views: 30

Answers (1)

Jose Manuel
Jose Manuel

Reputation: 183

The problem is that the defined route uses a PUT method and the form route uses a POST method.

Either change the route to be POST with post 'survey_requests/confirm', to: 'survey_requests#confirm' or add method: :put to the form form_for @survey_request, url: survey_requests_confirm_path, method: :put so the methods match.

Upvotes: 1

Related Questions