Luigi
Luigi

Reputation: 5603

Rails - No route matches POST

I have the following in my routes.rb file:

post 'report/mnps/generate' => 'report#mnps_generate', as: 'report_mnps_generate'

Then, in my reports/mnps.html.erb view I have this:

<%= button_to report_mnps_generate_path %>

However, this button redirects to a post method at reports/mnps. Why is this button redirecting there instead of report/mnps/generate?

EDIT

rake routes returns:

          Prefix Verb URI Pattern                             Controller#Action
              root GET  /                                       home#index
      report_index GET  /report(.:format)                       report#index
       report_mnps GET  /report/mnps(.:format)                  report#mnps
report_mnps_generate POST /report/mnps/generate(.:format)         report#mnps_generate

Upvotes: 1

Views: 97

Answers (1)

KappaNossi
KappaNossi

Reputation: 2716

The definition of button_to states that the first parameter is its name, which is usually used as label. See here:

http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to

To get a link to the page, you need to write the button like this

button_to('Clickme!', report_mnps_generate_path)

The reason why it loaded the page you stated is that the button is actually on that very same page and is simply reloading it since no other destination was defined in your button_to call.

Upvotes: 1

Related Questions