Html 将 html5 数据属性与 rails content_tag helper 一起使用的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4258512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to use html5 data attributes with rails content_tag helper?
提问by Cory Schires
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?
我有点喜欢后者,但两者似乎都有点恶心。有人知道更好的方法吗?
回答by stephencelis
Rails 3.1 ships with built-in helpers:
Rails 3.1 附带内置帮助程序:
http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag
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"]" />
回答by Eimantas
Have you tried using quotes with symbol? Something like:
您是否尝试过使用带符号的引号?就像是:
:"data-foo" => :bar
回答by Cory Schires
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)
回答by coloradoblue
JQuery Air (codeschool.com) Level 1, Example 1
JQuery Air (codeschool.com) 级别 1,示例 1
Codeschool/platform-independent version
Codeschool/平台独立版本
<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
Rails 版本
<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>
回答by coisnepe
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>
回答by rodrigob
You can always create you own helper function so then you can write
您始终可以创建自己的辅助函数,然后您就可以编写
<%= div_data_tag the_id, some_text, some_data %>