Forum: Ruby on Rails rails way to make a tag class active in view?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d3b058e892ec1e987a60c0b39665581?d=identicon&s=25 mixplate (Guest)
on 2007-06-01 16:55
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.

    <ul>
      <!-- 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>
    </ul>
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-01 19:08
(Received via mailing list)
On Jun 1, 2007, at 10:55 AM, mixplate wrote:
>       <li><a href="index.html">4</a></li>
>       <li><a href="index.html">5</a></li>
>     </ul>
>
> --

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

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

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

Gives:
<ul>
<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>
</ul>

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

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-01 19:08
(Received via mailing list)
(resending without being signed, sorry if the original shows up)
On Jun 1, 2007, at 10:55 AM, mixplate wrote:
>       <li><a href="index.html">4</a></li>
>       <li><a href="index.html">5</a></li>
>     </ul>
>
> --

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

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

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

Gives:
<ul>
<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>
</ul>

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

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
5d3b058e892ec1e987a60c0b39665581?d=identicon&s=25 mixplate (Guest)
on 2007-06-01 19:31
I Rob,Thank you for your reply. Actually, the code i pasted would be
more like this:

    <ul>
      <!-- The link you call "active" will show up as a darker tab -->
      <li> <% link_to :action => get_category, :category => 'pork' %>
</li>
          <li class="active"> <span>3</span> </li>
       <li><% link_to :action => get_category, :category => 'beef'
%></li>

    </ul>


so @examples would be an array of my category names?


Rob Biedenharn wrote:
> (resending without being signed, sorry if the original shows up)
> On Jun 1, 2007, at 10:55 AM, mixplate wrote:
>>       <li><a href="index.html">4</a></li>
>>       <li><a href="index.html">5</a></li>
>>     </ul>
>>
>> --
>
> Assuming that the "index.html" would actually be based on the 1, 2,
> etc.:
>
> <ul>
> <% for example in @examples %>
> <li><%= link_to_unless_current example, example_path(example) do |text|
>            content_tag(:span, text, :class => 'active')
>          end %></li>
> <% end %>
> </ul>
>
> When @examples = [1,2,3,4,5]
> and  example_path(3) => "/example/3"
>
> Gives:
> <ul>
> <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>
> </ul>
>
> 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
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-01 19:46
(Received via mailing list)
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
work).

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
cleaner)

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.

-Rob

On Jun 1, 2007, at 1:31 PM, mixplate wrote:
>
>>>       <li><a href="index.html">5</a></li>
>> text|
>> <li><a href="/example/1">1</li>
>> -Rob
>>
>> Rob Biedenharn    http://agileconsultingllc.com
>> Rob@AgileConsultingLLC.com
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
+1 513-295-4739
Skype:  rob.biedenharn
This topic is locked and can not be replied to.