I have a Rails 3 app and am writing an "administrative" partial I'd like to include in views across many different models. The partial will include resourceful links (e.g. new, index, show), but my design fails if the resource has a route (e.g. index) disabled in routes.rb.
A sample view (views/my_resources/show.html.erb):
<% provide(:title, 'My Resource') %>
<div style="float:right;">
<%= render '/shared/resource_menu' %>
</div>
<h1>My Resource</h1>
<%= @my_resource.name %>
Excerpt from /shared/_resource_menu.html.erb:
<div class="submenu">
<b><%= File.basename(params[:controller]).pluralize.titleize %></b><br />
<% if params[:action] != 'index' %>
<%= link_to 'Index', url_for(:controller => params[:controller], :action => 'index', :only_path => true) %><br />
<% end %>
<% if params[:action] != 'new' %>
<%= link_to 'New', url_for(:controller => params[:controller], :action => 'new', :only_path => true) %><br />
<% end %>
</div>
Currently, I've hacked it so that each conditional looks like:
<% if params[:action] != 'index' && !@omit_index_in_resource_menu %>
and I set the @omit_index_in_resource_menu
by hand in the relevant controllers, but that's clunkier than I would prefer.
Can I instead do something more elegant like:
<% if params[:action] != 'index' && controller.actions.include?('index') %>
Maybe this would help you:
UsersController.action_methods
(it's a class method)
or
UsersController.new.available_action? :index
(it's an instance method)
More info: http://api.rubyonrails.org/classes/AbstractController/Base.html#method-i-action_methods