I have a navigation bar which is an unordered list with tabs as list elements. I'm drawing each individual tab (list item) using a view helper. I want to apply the active
class on each one based on a value provided by a view. Is this possible?
To elaborate, on the view helper I might have something like:
def tab_item(tab)
content_tag :li, :class => ('active' if some_variable == tab) do
link_to tab, "/#{tab}"
end
end
And in a specific view I would do:
<% somehow_set_some_variable('dash') %>
This would then make it so that some_variable
in the view helper is 'dash'
, making it so that that tab gains the 'active'
class.
Is this possible in any way? If not, is there another approach to this problem? Should I just set a variable on the controller?
I have seen articles and questions already which base the decision based on the current controller and/or action, but this is too restrictive so I am looking to see if I can take this approach. Another solution I came up with is to generate the entire list of tabs in one view helper which takes an argument for the tab to be activated, and I call this in each of the views. However, I'd rather generate the list in the layouts and activate a tab on a per-view basis.
Workaround: So far I have come up with a compromise, short of knowing a way of doing what I asked in this question. The application helper creates an li
tag and applies activate
if the passed in argument to the application helper matches instance variable @activate
.
def nav_tab(tab)
content_tag :li, :id => tab, :class => ('active' if tab == @activate) do
link_to tab, "/#{tab}"
end
end
Then in controller actions I simply set that variable, for example:
def index
# ...
@activate = 'dash'
end
This works, and it has the effect I was looking for: the nav bar is generated independently and then each 'action' can specify which tab to activate, if they want.
I am not sure if this is the best solution to this problem. I was trying to keep this stuff within views, like this question which accomplishes this for view-specific <title>
changes.