nicholas79171
nicholas79171

Reputation: 1273

How do I check which controller is used to render a partial in Rails?

I have a simple partial for my footer that looks like this:

<footer class="footer">
  <nav>
    <ul>
      <li><%= link_to "Download History", report_histories_path(format: "csv") %>
      <li><%= link_to "Help", help_path %></li>
      <li><%= link_to "About", about_path %></li>
    </ul>
  </nav>
</footer>

The first link allows the user to download some data used to power reports as a CSV, but I only want this to link to appear if the reports_controller is used to render the partial.

I've tried using

<% if params[:reports] %>
  <li><%= link_to "Download History", report_histories_path(format: "csv") %>
<% end %>

as well as

<% if current_page?(url_for(:controller => 'reports')) %>
  <li><%= link_to "Download History", report_histories_path(format: "csv") %>
<% end %>

but neither show the link.

Upvotes: 3

Views: 841

Answers (2)

Jay-Ar Polidario
Jay-Ar Polidario

Reputation: 6603

You could also use controller_name or action_name. Or specifically, to answer your question <% if controller_name == 'reports' %>

Upvotes: 2

EugZol
EugZol

Reputation: 6545

You can use params[:controller] for that. Also, params[:action] will contain current action.

Upvotes: 5

Related Questions