Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add aria-label attribute to a link in a view?

I'm trying to add the aria-label attribute to a link to make it more accessible. When I'm doing this, it works as expected:

<a href="/" class="site-name <%= is_active('home') %>" aria-label="<%= get_aria_label_current_page('home') %>">Version Postman</a>

But this doesn't:

<%= link_to t('nav.projects'), projects_path, class: is_active('projects'), aria-label: get_aria_label_current_page('home') %>

I get an "unexpected tLABEL" syntax error. Anyone knows what's the problem?

Thanks.

like image 838
actaram Avatar asked Nov 09 '14 18:11

actaram


2 Answers

It's the dash on the label creating the problem. Try this instead:

<%= link_to t('nav.projects'),
            projects_path, class: is_active('projects'),
            'aria-label' => get_aria_label_current_page('home') %>

Update

In ruby 2.2 now you could do:

'aria-label': get_aria_label_current_page('home')
like image 77
Mario Pérez Avatar answered Oct 30 '22 10:10

Mario Pérez


As of at least Rails 5.2 this works too:

<%= link_to t('nav.projects'),
            projects_path,
            class: is_active('projects'),
            aria: { label: get_aria_label_current_page('home') } %>

This is similar to how data-* attributes work, which is nice since you can add more than one and have them grouped.

This may work on earlier versions but I have not checked.

like image 24
Todd Avatar answered Oct 30 '22 11:10

Todd