Read more

Nice way to set data attributes when creating elements with Rails helpers

Henning Koch
July 21, 2012Software engineer at makandra GmbH

You can say this in helpers like link_to and content_tag:

= link_to 'Label', root_url, :data => { :foo => 'bar', :bam => 'baz' }
Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

This will produce:

<a href="/" data-foo="bar" data-bam="baz">Label</a>

Only works in Rails 3. In Rails 2 you do

= link_to 'Label', root_url, 'data-foo' => 'bar', 'data-bam' => 'baz' }
Posted by Henning Koch to makandra dev (2012-07-21 16:35)