如何创建包含 class 和数据字段的 link_to 标签?

How do I create a link_to tag with both class and data fields?

我想创建一个 Rails link_to 标签,它最终会生成一个对话框,所以它只是 link 到“#”。但是,我希望它具有 class 和数据属性。但是当我尝试这个

<%= link_to "What's This?", "#", {class: "more_info"}, :data => { :more_info => 'mt_hashes_info' } %>

我收到错误消息

wrong number of arguments (given 4, expected 0..3)

构造此 link 的正确方法是什么?

这是来自 Redmine 核心的示例:

link_to(l(:button_archive), archive_project_path(project, :status => params[:status]), :data => {:confirm => l(:text_are_you_sure)}, :method => :post, :class => 'icon icon-lock')

对 run-on 声明表示抱歉;你知道红矿!但请注意 :data 和 :class 都是同一个 Hash...

的对等体

它应该是这样的:

<%= link_to "What's This?", "#", {:class => "more_info", :data => { :more_info => 'mt_hashes_info' }} %>

Class 和数据都进入相同的选项散列。

你可以像这样简单地做到这一点

<%= link_to "What's This?", "#", class: "more_info", data: { more_info: "mt_hashes_info" } %>

生成的HTML

<a class="more_info" data-more-info="mt_hashes_info" href="#">What's This?</a>

带有 confirmation 对话框

<%= link_to "What's This?", "#", class: "more_info", data: { confirm: "Are you sure?" } %>

生成的HTML

<a class="more_info" data-confirm="Are you sure?" href="#">What's This?</a>

您可以找到更多 link_to