link_to_unless_current with block gives a weird "0"

355 views
Skip to first unread message

Tobias

unread,
Nov 16, 2009, 10:54:02 AM11/16/09
to Haml

Hi!

I have a link to the users profile in my meta navigation and I am
looking for an easy, automatic way to tell the user, that the is
currently on the page that the link describes.

This is good but not good enough, since I cannot assign an "active"
class in case the page is the current page.
= link_to_unless_current "your profile", user_url(current_user)

According to the documentation (http://github.com/rails/rails/blob/
9e29c084eb0551eb18b666c8df53b9985af8aa16/actionpack/lib/action_view/
helpers/url_helper.rb#L354) there is a solution for that:
= link_to_unless_current "Dein Profil", user_url(current_user) do |
name|
%strong.current= name

This works fine with one problem: It generates an unwanted "0" after
the tag.

Output if url is current:
<strong class="current"> Dein Profil </strong>
0

Where does this "0" come from and how do I get rid of it?

Thanks a lot!
Tobias

Nathan Weizenbaum

unread,
Nov 16, 2009, 3:33:57 PM11/16/09
to ha...@googlegroups.com
Notice that in the ERB examples for link_to_unless don't actually include any raw HTML text within the block; that is, the entire block is within the <%= ... %>. You need to do the same with Haml if you want to use the helper. That means that you can't use = or any of the HTML-generating Haml code; you have to use

  - "<strong class='current'>#{h name}</strong>"

instead.


--

You received this message because you are subscribed to the Google Groups "Haml" group.
To post to this group, send email to ha...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/haml?hl=.



Tobias

unread,
Nov 26, 2009, 2:16:31 AM11/26/09
to Haml


On 16 Nov., 21:33, Nathan Weizenbaum <nex...@gmail.com> wrote:
> Notice that in the ERB examples for link_to_unless don't actually include
> any raw HTML text within the block; that is, the entire block is within the
> <%= ... %>. You need to do the same with Haml if you want to use the helper.
> That means that you can't use = or any of the HTML-generating Haml code; you
> have to use
>
> - "<strong class='current'>#{h name}</strong>"
>
> instead.

Thanks a lot, Nathan!

I changed the code to:
= link_to_unless_current 'Dein Profil', user_url(current_user) do |
name| "<strong class='current'>#{name}</strong>" end
which works fine!

Regards -Tobias

Nathan Weizenbaum

unread,
Nov 26, 2009, 2:25:40 AM11/26/09
to ha...@googlegroups.com
Here's how I'd do it: http://gist.github.com/243304

--

You received this message because you are subscribed to the Google Groups "Haml" group.
To post to this group, send email to ha...@googlegroups.com.
To unsubscribe from this group, send email to haml+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/haml?hl=en.



Reply all
Reply to author
Forward
0 new messages