0

I am trying to refactor some of the code in my view

<% if controller.controller_name == "overview" %>
<div id="left-menu">
    <ul>
    <li>Office</li>
    <hr>
    <li><%= link_to "Overview", root_path %></li>
    <li><%= link_to "Personnel", personnel_path %></li>
    <li><%= link_to "Results", results_path %></li>
    <li><%= link_to "Statistics", statistics_path %></li>
    </ul>
    <ul>
    <li>Economy</li>
    <hr>
    <li>Finances</li>
    <li>Contracts</li>
    <li>Transfers</li>
    <li>Sponsors</li>
    </ul>               
</div>
<% elsif controller.controller_name == "market" %>
<div id="left-menu">
    <ul>
    <li>Items</li>
    <hr>
    <li><%= link_to "Engines", market_engines_path %></li>
    <li><%= link_to "Weapons", market_weapons_path %></li>
    <li><%= link_to "Armor", market_armor_path %></li>
      <li><%= link_to "Chips", market_chips_path %></li>
    </ul>
    <ul>
    <li>Personnel</li>
    <hr>
    <li><%= link_to "Drivers", market_drivers_path %></li>
    <li><%= link_to "Servicemen" %></li>
    <li><%= link_to "Programmers" %></li>
    <li><%= link_to "Managers" %></li>
    </ul>               
</div>
<% end %> 

Where each <li> corresponds to a method in the controller. I would like to be able to add new methods to my controllers, and then have them dynamically inserted in the view. So is there a way to iterate over the methods in a controller?

vee
  • 38,255
  • 7
  • 74
  • 78
manis
  • 731
  • 1
  • 13
  • 24
  • 1
    Duplicate of http://stackoverflow.com/questions/8686441/how-to-get-list-of-controllers-and-actions-in-ruby-on-rails ? – joshua.paling Mar 29 '14 at 12:08

1 Answers1

0

As per the answer provided in the referenced question, Controller.action_methods seems to be the method you need, however, to integrate into your code is as follows:

<% lists = %w(items personell) %>
<% for list in lists do %>
    <ul>
        <li><%= list.titleize %></li>
        <hr>
        <% items = MarketsController.action_methods %>
        <% for item in items do %>
            <li><%= link_to item.titleize, eval("market_#{item}_path") %></li>
        <% end %>
    </ul>
<% end %>
Richard Peck
  • 76,116
  • 9
  • 93
  • 147