I have a view that can be accessed by multiple controllers. For the most part, they display the same information, but this information is tweaked slightly from one controller to the next. I need a way to distinguish which controller is being used at any given moment. I do this by declaring an “@context” variable in the controller, such as:
@context = "index"
or
@context = "show"
I can then easily check within the view which controller is being used. For instance:
<% if @context == "index" %>
<h1>Index</h2>
<% elsif @context == "show %>
<h2>Show</h2>
<% end %>
However, this feels hacky, and I’m certain there’s a cleaner solution. Does anyone know of one?
You can use controller_name and action_name helpers, e.g.