rails way to make a tag class active in view?

(resending without being signed, sorry if the original shows up)

for example 3 is in active state. right now all i can think of is in
each action, just set a variable like @active = 'active' or '', but not
sure what to do with the span.

      <!-- The link you call "active" will show up as a darker tab -->
      <li><a href="index.html">1</a></li>
      <li><a href="index.html">2</a></li>
      <li class="active"> <span>3</span> </li>
      <li><a href="index.html">4</a></li>
      <li><a href="index.html">5</a></li>


Assuming that the "index.html" would actually be based on the 1, 2, etc.:

<% for example in @examples %>
<li><%= link_to_unless_current example, example_path(example) do |text|
           content_tag(:span, text, :class => 'active')
         end %></li>
<% end %>

When @examples = [1,2,3,4,5]
and example_path(3) => "/example/3"

<li><a href="/example/1">1</li>
<li><a href="/example/2">2</li>
<li><span class='active'>3</span></li>
<li><a href="/example/4">4</li>
<li><a href="/example/5">5</li>

Is that what you're looking for? The link_to_if, link_to_unless, link_to_unless_current helpers take a block that is passed the text of the link when the condition isn't satisfied for making the link.


Rob Biedenharn http://agileconsultingllc.com

Well, if you need to have
<li class="active"><span> ...
rather than
<li><span class="active> ...
Then you might have to restructure a bit (a listitem_link_to() might

If you can live with decorating the contents of the span rather than
the li, you can do this:

<li><%= link_to_unless_current(example,
                                :action => 'get_category',
                                :category => example) do |text|
            content_tag(:span, text, :class => 'active')
          end %></li>

and if the current page's URL path matches what the parameters
construct, the link will be rendered.

You might also do: (nevermind, the alternative gets too ugly to even
consider. Use a custom helper method if you want to keep your view

Look at the documentation for current_page? and the various link_to
helpers and you ought to be able to figure it out. If you need more
help, show us your actual view and controller code snippets.