109

The issue, of course, is that ruby symbols don't like hyphens. So something like this obviously won't work:

content_tag(:div, "Some Text", :id => "foo", :data-data_attr => some_variable)

One option is to use a string rather than a symbol:

content_tag(:div, "Some Text", :id => "foo", 'data-data_attr' => some_variable)

Or I could just interpolate:

"<div id='foo' data-data_attr='#{some_variable}'>Some Text</div>".html_safe

I sorta prefer the later but both seem a little gross. Anyone know of a better way?

Cory Schires
  • 2,146
  • 2
  • 14
  • 26

6 Answers6

147

Rails 3.1 ships with built-in helpers:

http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag

E.g.,

tag("div", :data => {:name => 'Stephen', :city_state => %w(Chicago IL)})
# => <div data-name="Stephen" data-city-state="[&quot;Chicago&quot;,&quot;IL&quot;]" />
stephencelis
  • 4,954
  • 2
  • 29
  • 22
  • This actually just appends things onto my URL. It doesn't add a data attribute. – Jim Wharton Dec 06 '11 at 02:05
  • 2
    Of course it does.... But if you are using a helper which takes both a url hash AND a html options hash you have to explicitly wrap both hashes in curly brackets {}. link_to for example: link_to "label", {:action => blub}, {:data => {:foo => :bar}, :class => "test"} – reto May 25 '12 at 09:41
  • This works: 'data-bv-notempty-message'=>"The username is required" – Ivan Oct 17 '14 at 14:21
66

Have you tried using quotes with symbol? Something like:

:"data-foo" => :bar
Eimantas
  • 48,927
  • 17
  • 132
  • 168
11

A helper's not a bad idea but seems a bit of an overkill for what's essentially me being fusy about syntax. I suppose there's nothing built into rails which is what I was hoping for. I'll just use this:

content_tag(:div, "Some Text", :id => "foo", 'data-data_attr' => some_variable)
Cory Schires
  • 2,146
  • 2
  • 14
  • 26
9

Building on previous answers, here's the canonical way to do it now:

content_tag(:div, "Some Text", id: "foo", data: { attr: some_variable })
content_tag(:div, "Some Text", id: "foo", data: { "other-attr" => some_variable })

Which generates:

<div id="foo" data-attr="some variable">Some Text</div>
<div id="foo" data-other-attr="some variable">Some Text</div>
coisnepe
  • 480
  • 8
  • 18
8

JQuery Air (codeschool.com) Level 1, Example 1

Codeschool/platform-independent version

<section id="tabs">
  <ul>
    <li><a href="#2012-09-27" data-flights="6">Sep 27</a></li>
    <li><a href="#2012-09-28" data-flights="5">Sep 28</a></li>
    <li><a href="#2012-09-29" data-flights="5">Sep 29</a></li>
  </ul>
</section>

Rails Version

<section id="tabs">
  <ul>
    <li><%= content_tag(:a, "Sep 27",:href=> "#2012-09-27", :data => { :flights => "6" } ) %></li>
    <li><%= content_tag(:a, "Sep 28",:href=> "#2012-09-28", :data => { :flights => "5" } ) %></li>
    <li><%= content_tag(:a, "Sep 29",:href=> "#2012-09-29", :data => { :flights => "5" } ) %></li>
  </ul>
</section>
coloradoblue
  • 625
  • 7
  • 11
-4

You can always create you own helper function so then you can write

<%= div_data_tag the_id, some_text, some_data %>
rodrigob
  • 2,891
  • 3
  • 30
  • 34