Ruby-on-rails Rails link_to 分配类和 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14284633/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:17:04  来源:igfitidea点击:

Rails link_to assigning class and id

ruby-on-railslink-to

提问by Dudo

<%= link_to event do  %>
  #bunch of stuff making up the partial.
<% end %>

So I'm trying to assign an ID and a class to each item in a partial. I've seen where you have to call the full link_tofunction like <%= link_to event, { controller: :controller, action: :action }, {class: 'someClass', id: 'someId' } %>.

所以我试图为部分中的每个项目分配一个 ID 和一个类。我已经看到您必须在何处调用完整的link_to函数,例如<%= link_to event, { controller: :controller, action: :action }, {class: 'someClass', id: 'someId' } %>.

That's not working for me, because of the do block, methinks? Ideas?

这对我不起作用,因为 do 块,我想?想法?

回答by Ben

Does this work for you?

这对你有用吗?

<%= link_to event, id: "an-id", class: "some-class" do  %>
  #bunch of stuff making up the partial.
<% end %>

回答by Hitham S. AlQadheeb

You can do

你可以做

<%= link_to 'event', { controller: :pages, action: :home }, class: 'someClass', id: 'someId'  %>

which will give you

这会给你

<a href="/the_generated_path" class="someClass" id="someId">event</a>

To make 'event'actually an HTML div you can do

'event'实际制作HTML div,您可以执行以下操作

<%= link_to(raw("<div>..</div>"), ....)  %>