Best way to use html5 data attributes with rails content_tag helper? Best way to use html5 data attributes with rails content_tag helper? ruby-on-rails ruby-on-rails

Best way to use html5 data attributes with rails content_tag helper?


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="["Chicago","IL"]" />


Have you tried using quotes with symbol? Something like:

:"data-foo" => :bar


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)