在link_to属性中使用连字符?

时间:2010-07-09 16:15:59

标签: ruby-on-rails hash html5 anchor

在我的Rails应用中,我需要为锚标记上的自定义data-*属性设置一个值。但是,如果我没有弄错,哈希不能有连字符。我基本上想要这样做:

<%= link_to 'Example', example_path, :class => 'something', :data-id => '15' %>
但是,

:data-id无效。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:9)

IIRC,出于此目的,哈希和字符串是等效的,因此您可以使用“data-id”而不是:data-id。但是,从未检查过这种特殊的方法,所以没有保证。

答案 1 :(得分:7)

我认为在Rails 3中你可以做到

link_to "Click Here", root_path, :data => { :id => @model.id }

表示所有数据属性。

相关问题