简体   繁体   中英

Jump Links with HAML on a RoR website

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.

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.

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM