How do I add the aria-label attribute to a link in a view?
I am trying to add an aria-label attribute to a link to make it more accessible. When I do 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 is not the case:
<%= link_to t('nav.projects'), projects_path, class: is_active('projects'), aria-label: get_aria_label_current_page('home') %>
I am getting the "unexpected tLABEL" syntax error. Does anyone know what the problem is?
Thank.
+4
source to share
2 answers
This is the feature on the label that creates the problem. Try this instead:
<%= link_to t('nav.projects'), projects_path, class: is_active('projects'), 'aria-label' => get_aria_label_current_page('home') %>
Refresh
In ruby ββ2.2 you can now do:
'aria-label': get_aria_label_current_page('home')
+11
source to share
At least on 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 are data-*
, which is good since you can add more than one and group them.
This may work on earlier versions, but I haven't tested it.
0
source to share