Matt Huggins
Matt Huggins

Reputation: 83289

Using hyphen in link_to property?

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?

Upvotes: 4

Views: 1024

Answers (2)

user791715
user791715

Reputation: 123

I think in Rails 3 you can do

link_to "Click Here", root_path, :data => { :id => @model.id }

for all data attributes.

Upvotes: 7

Nikita Rybak
Nikita Rybak

Reputation: 68016

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.

Upvotes: 9

Related Questions