views:

27

answers:

2

In my Rails app, I need to set a value for a custom data-* attribute on an anchor tag. However, hashes can't have hyphens if I'm not mistaken. I essentially want to do this:

<%= link_to 'Example', example_path, :class => 'something', :data-id => '15' %>

:data-id is not valid, however. What can I do to work around this?

+4  A: 

IIRC, for such purposes hashes and strings are equivalent, so you can use "data-id" instead of :data-id. Never checked for this particular method, though, so no guarantees.

Nikita Rybak
Also `:"data-id"` is a valid symbol, but it looks ugly and I would never do that personally..
Karl
Awesome, thanks!
Matt Huggins
@Karl - good to know that too, thanks :)
Matt Huggins
A: 

Also, the naming conventions would encourage you to name it "data_id" instead.

normalocity
That's not HTML5 compliant: http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data
Matt Huggins