Possible issue with link_to_unless_current

60 views
Skip to first unread message

G. Sobrinho

unread,
Dec 23, 2009, 2:53:41 PM12/23/09
to ha...@googlegroups.com
Hi,

I'm using HAML on my projects and I having a trouble with
link_to_unless_current.

Reading the Rails documentation you can use:

<%=
  link_to_unless_current("Comment", { :controller => 'comments',
:action => 'new}) do
    link_to("Go back", { :controller => 'posts', :action => 'index' })
  end
%>

I want a similar behavior, like this:

= link_to_unless_current('Contact', contact_index_path) { link_to
'Contact', contact_index_path, :class => 'active' }

Not that this works but the line is too big. To make the line more
readable i'm trying to use:

= link_to_unless_current('Contact', contact_index_path) do
  = link_to 'Contact', contact_index_path, :class => 'active'

But this create a unexpected "7" on the source. If i'm outside contact
page this works fine. But if i'm on contact page (which will use the
block) the rendered HTML render this:

<ul>
<li><a href="/">Home</a></li>
<li><a href="/pages/about-us">About US</a></li>
<li>
<a href="/contact" class="active">Contact</a>
7
</li>

</ul>


It's a HAML related issue or I need to do it in another way?

--
Cordialmente,

Gabriel Sobrinho
Diretor de desenvolvimento

Hite - Comunicação Digital e Mídia Interativa
http://www.hite.com.br/

+55 31 8775 8378

Rhett Sutphin

unread,
Dec 23, 2009, 3:09:27 PM12/23/09
to ha...@googlegroups.com
Hi Gabriel,

On Dec 23, 2009, at 1:53 PM, G. Sobrinho wrote:

> Hi,
>
> I'm using HAML on my projects and I having a trouble with
> link_to_unless_current.
>
> Reading the Rails documentation you can use:
>
> <%=
> link_to_unless_current("Comment", { :controller => 'comments',
> :action => 'new}) do
> link_to("Go back", { :controller => 'posts', :action => 'index' })
> end
> %>
>
> I want a similar behavior, like this:
>
> = link_to_unless_current('Contact', contact_index_path) { link_to
> 'Contact', contact_index_path, :class => 'active' }
>
> Not that this works but the line is too big. To make the line more
> readable i'm trying to use:
>
> = link_to_unless_current('Contact', contact_index_path) do
> = link_to 'Contact', contact_index_path, :class => 'active'

Try:

= link_to_unless_current('Contact', contact_index_path) do

- link_to 'Contact', contact_index_path, :class => 'active'

Note the '-' instead of '=' in the second line. It means that that
line should be executed, but the result shouldn't be appended to the
page. This is equivalent to the ERB you included above and the first
(single line) haml version.

Rhett

>
> But this create a unexpected "7" on the source. If i'm outside contact
> page this works fine. But if i'm on contact page (which will use the
> block) the rendered HTML render this:
>
> <ul>
> <li><a href="/">Home</a></li>
> <li><a href="/pages/about-us">About US</a></li>
> <li>
> <a href="/contact" class="active">Contact</a>
> 7
> </li>
>
> </ul>
>
>
> It's a HAML related issue or I need to do it in another way?
>
> --
> Cordialmente,
>
> Gabriel Sobrinho
> Diretor de desenvolvimento
>
> Hite - Comunicação Digital e Mídia Interativa
> http://www.hite.com.br/
>
> +55 31 8775 8378
>

> --
>
> 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
> .
>
>

Tobias

unread,
Dec 24, 2009, 11:31:33 AM12/24/09
to Haml

Hi!

On 23 Dez., 20:53, "G. Sobrinho" <gabriel.sobri...@gmail.com> wrote:


> = link_to_unless_current('Contact', contact_index_path) do
>   = link_to 'Contact', contact_index_path, :class => 'active'
>
> But this create a unexpected "7" on the source. If i'm outside contact
> page this works fine. But if i'm on contact page (which will use the
> block) the rendered HTML render this:

I had the same question.
The solution is described here:
http://groups.google.com/group/haml/browse_thread/thread/52e62ef501c504a3

Unfortunatelly you have to write the whole method in one line and
escape the block with "" to make it readable.
= link_to_unless_current 'Profile', user_url(current_user) do |
name| "<strong class='current'>#{name}</strong>" end

Regards
Tobias


G. Sobrinho

unread,
Dec 25, 2009, 11:22:52 AM12/25/09
to Haml
This doesn't work because link_to_unless_current expects the result of
block and using "-" doesn't return the output to the block.

Norman Clarke

unread,
Dec 25, 2009, 11:48:45 AM12/25/09
to ha...@googlegroups.com
I think the easiest solution for you in this case is to make a helper. Haml makes including lots of Ruby code difficult on purpose, because it is optimized more for stucture than for logic or content. Making a helper is usually the path of least resistance.

Regards,

Norman

Norman Clarke

unread,
Dec 25, 2009, 8:50:08 PM12/25/09
to ha...@googlegroups.com
BTW, if all you want to do is add the class "active" to current links, rather than using the default block in "link_to_unless_current", you may wish to simply wrap the "link_to" method and use the "current_page?" method directly, which I think is a cleaner way to accomplish what you're trying to do:

def nav_link(name, options = {}, html_options = {}, &block)
  if current_page?(options)
    html_options[:class] ? html_options[:class] <<  " active" : html_options[:class] = "active"
  end
  link_to(name, options, html_options, &block)
end


--Norman


On Fri, Dec 25, 2009 at 1:22 PM, G. Sobrinho <gabriel....@gmail.com> wrote:

G. Sobrinho

unread,
Dec 26, 2009, 11:34:50 AM12/26/09
to Haml
Thanks Norman, I will use the helper

Best regards

On Dec 25, 11:50 pm, Norman Clarke <com...@gmail.com> wrote:
> BTW, if all you want to do is add the class "active" to current links,
> rather than using the default block in "link_to_unless_current", you may
> wish to simply wrap the "link_to" method and use the "current_page?" method
> directly, which I think is a cleaner way to accomplish what you're trying to
> do:
>
> def nav_link(name, options = {}, html_options = {}, &block)
>   if current_page?(options)
>     html_options[:class] ? html_options[:class] <<  " active" :
> html_options[:class] = "active"
>   end
>   link_to(name, options, html_options, &block)
> end
>
> --Norman
>

> > haml+uns...@googlegroups.com <haml%2Bunsu...@googlegroups.com>


> > > > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/haml?hl=en
> > > > .
>
> > --
>
> > 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 <haml%2Bunsu...@googlegroups.com>.

stephen murdoch

unread,
Jan 4, 2010, 9:22:43 PM1/4/10
to Haml
I normally just do this:

= link_to_unless_current "Home", root_path do |link|
- content_tag 'span', link, :class=>'current'

It works. Unless of course you NEED to use 'link_to' inside your
block. Is there any particular reason for that? If you are setting a
link to 'active' then you are surely making it unclickable.... So
it's not a link anymore.

G. Sobrinho

unread,
Jan 5, 2010, 12:07:52 PM1/5/10
to Haml
Not exactly... I want to add the class "active" to the current link.

I created a helper which check the current page using current_page?,
used by link_to_unless_current.

On 5 jan, 00:22, stephen murdoch <stephenjamesmurd...@gmail.com>
wrote:

Reply all
Reply to author
Forward
0 new messages