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