EastsideDev
EastsideDev

Reputation: 6649

Error using a button in view to trigger action

Rails 5.2

In my routes.rb, I have the following:

put 'lights/reset_node', to: 'lights#reset_node'

When I run rake routes, I get:

lights_reset_node PUT    /lights/reset_node(.:format)  lights#reset_node

In lights_controller.rb, I have:

def reset_node #TODO write method end

In my view, I have the following:

= link_to lights_reset_node_path(:node => @node), :method => :put do
  button.btn.btn-secondary type="button"
    = t('device.show.reset_node_button')

However, when I click on the button, I get the following:

Unknown action
The action 'update' could not be found for LightsController

Upvotes: 1

Views: 37

Answers (1)

Rockwell Rice
Rockwell Rice

Reputation: 3002

Your issue, just to explain more in-depth, was that routes inside routes.rb are read in order. So when a route with the same URL is hit first it goes with that route and never goes further down the file. So make sure your routes in the file are in the correct order.

In your case, this was placed higher in the routes.rb file, and so that is why it was looking at the wrong route.

resources :lights

As you stated, once you moved that down below your other route put 'lights/reset_node', to: 'lights#reset_node' it worked because now this was higher up in the order.

Upvotes: 1

Related Questions