1

Super simple question here, yet I can't seem to find a definitive answer.

I'm currently working a page where a list of links will lead to different areas of the page. I know how to do this is regular HTML (with the "name" field) but I'm not sure how to do it in HAML. I only started working with it a short time ago. I tried doing it like this, which isn't working.

.li 
   = link_to 'Information', '#info'

The section I'm trying to link down to:

.h2 INFORMATION { name: 'info' }

There's a weird scarcity of info regarding how to solve this? If anyone could point me in the right direction, I'd really appreciate it.

Leia_Organa
  • 1,894
  • 7
  • 28
  • 48

2 Answers2

2

Rather than this:

.h2 INFORMATION { name: 'info' }

you want this:

.h2{name: 'info' } INFORMATION

The attribute hash needs to be right next to the class name. This generates:

<div class='h2' name='info'>INFORMATION</div>

with the name attribute. In your code the { name: 'info' } is just included as part of the content of the div.

matt
  • 78,533
  • 8
  • 163
  • 197
1

The link_to helper can produce links with anchors:

link_to "Comment wall", profile_path(@profile, anchor: "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
.block
  %h2#block1 Block 1
  = link_to "Block 2", route_path(anchor: "#block2")
.block
  %h2#block2 Block 2
  = link_to "Block 1", route_path(anchor: "#block1")

As a side note: I would recommend using the id attribute instead of the name attribute on your headings/sections. A lengthy discussion and answer as to why can be found in this thread: HTML anchors with name or id?

Patrik Affentranger
  • 13,245
  • 2
  • 22
  • 25