0

I'm new to Ruby on Rails. I'm building out an app that does custom operations on defined nodes (the object). Here's my table:

<% @nodes.each do |node| %>
    <tr>
      <td><%= node.name %></td>
      <td><%= link_to 'Bootstrap', node_path(node),
              method: :bootstrap %>
      </td>
    </tr>
  <% end %>

"Bootstrap" is the custom code that I want to run. I'm defined a custom method within my Controller:

class NodesController < ApplicationController
  def bootstrap
     ......
  end
  ........
end

How do I tie in my custom code to my Rails app so that when a user clicks on the link the code will be run against that object?

1 Answer 1

1

In config/routes.rb, you probably have:

resources :nodes

Change this to:

resources :nodes do
  get 'bootstrap', on: :member
end

Then, run rake routes to see that you now have a new route method, bootstrap_node_path(node), and will link to /nodes/:id/bootstrap.

I recommend this over the other approach as it keeps your route details together, but that's just my personal opinion. I usually resort to custom routes as a last resort.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up adding it as a member resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.