eve*_*ans 104 html5 ruby-on-rails ruby-on-rails-3
如何data-*在我的link_to帮助器(Rails)中使用html5 attrubute
API说我必须使用这种格式,link_to(body, url, html_options = {})但是当我把它放在html_options中时我有一个错误
例如:
link_to "whatever", @whatever_path, { class: 'my_class', data-tooltip: 'what I want' }
Run Code Online (Sandbox Code Playgroud)
set*_*rgo 213
只是传递它们... Rails有一个默认的:data哈希
= link_to body, url, :data => { :foo => 'bar', :this => 'that' }
Run Code Online (Sandbox Code Playgroud)
一个问题 - 如果包含短划线,则必须用引号括住符号:
:data => { :'foo-bar' => 'that' }
Run Code Online (Sandbox Code Playgroud)
更新:在Rails 4中,下划线自动转换为破折号,因此您可以这样做:
:data => { :foo_bar => 'that' }
Run Code Online (Sandbox Code Playgroud)
或者你可以直接写它:
= link_to body, url, :'data-foo' => 'bar', :'data-this' => 'that'
Run Code Online (Sandbox Code Playgroud)
更新2:正如评论中所指出的,Ruby 1.9+允许这种语法,有些人认为它更清晰:
{ data: { foo: "bar" } }
Run Code Online (Sandbox Code Playgroud)
data-通过执行以下操作添加属性:
link_to "Hello", hello_path, :"data-attribute" => "yeah!"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64300 次 |
| 最近记录: |