user938363
user938363

Reputation: 10350

Getting a routing error for form_tag in Rails 3.2

An custom action mass_onboard_result was declared in routes.rb for engine_config:

collection do
  put :mass_onboard_result
end

In controller for engine configs, there is definition for mass_onboard_result:

def mass_onboard_result
   code here
end

Here is the view which starts with form_tag:

<%= form_tag mass_onboard_result_engine_configs_path do %>
  .......
  <%=submit_tag 'Save' %>
<% end %>

When clicking Save on the form, there is an routing error:

No route matches [POST] "/onboard_data_upload/engine_configs/mass_onboard_result"

Try running rake routes for more information on available routes.

In output of rake routes, there is:

mass_onboard_result_engine_configs PUT    /engine_configs/mass_onboard_result(.:format)

What could cause the error. Is it the declaration in routes.rb?

Upvotes: 0

Views: 79

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

The error is written here:

No route matches [POST]...

A typical problem for Rails devs (especially ones starting out), is to overlook the importance of the HTTP verb in the routes structure.

--

Fix

When you use a form, it will default to sending the data via the POST verb. However, your routes have clearly defined your route as using the PUT verb:

collection do
  put :mass_onboard_result
end

To fix this, you either need to change the route to accept POST responses, or your form to send a PUT request:

#config/routes.rb
collection do
   match :mass_onboard_result, via [:post, :put]
end

-or-

#view
<%= form_tag your_path, method: :put %>

Upvotes: 1

sevenseacat
sevenseacat

Reputation: 25029

You have defined your route as a put route, but your form is creating a post request.

If you add method: :put into the form_tag options, is the problem resolved?

Upvotes: 1

Related Questions