link_to_unless_current

how can I use this link tag and add a class like "current" if it is the current link?

anrake wrote:

how can I use this link tag and add a class like "current" if it is the current link?

<p>Hello!</p> <p><%= link_to "Back to", { :action=> ""} %></p> <p><%= link_to "Back to", { :action => "" }, :popup => true %></p> <p><%= link_to "Back to", { :action => "" }, :popup => ['new_window', 'height=300,width=600'] %></p> <p><%= link_to "Back to", { :action => "" }, :confirm => "Are you sure?", :post => true %></p>

I'm sorry I don't see how this is doing what I need. Perhaps I should have explained more. Is there a way to using link_to_unless_current to add the class="current" attribute in a SPAN around the text instead of an A

Never recognized this function before, but as far as I understand the code, it is not really possible. You can use a block and parse the returned value, but this seems a bit more difficult than creating the link itself. But you can try.

Otherwise I would go for a css based solution to your problem. warp the link in a span and style the span the way you want. And then style the links within spans the way inactive links should appear.

I suppose so. Thanks for the effort. I was hoping someone had some code for a simple block I could just drop in.

In case of current link link_to_unless_current returns link text or whatever block passed to method yields, so:

<%= link_to_unless_current('some text', :action=>:someaction) {|link| "<span class=\"current\">#{link}</span>"} %>

This should work.

Regards, Rimantas

In case of current link link_to_unless_current returns link text or whatever block passed to method yields, so:

<%= link_to_unless_current('some text', :action=>:someaction) {|link| "<span class=\"current\">#{link}</span>"} %>

This should work.

Or better yet: <%= link_to_unless_current('some text', :action=>:someaction) {|link| content_tag 'span', link, :class => 'current'} %>

Regards, Rimantas

wow, that is perfect. Thank you very much !

But doesn't that put a span around all items, links and text?