简体   繁体   English

如何将 rails 代码添加到 div 标签中的 id 属性?

[英]How can I add rails code to id attribute in div tag?

I would like to do something like this: <div id="<%=service.name%>"我想做这样的事情: <div id="<%=service.name%>"

However, I just can't get it to work.但是,我无法让它发挥作用。 I need help!我需要帮助! I am trying to create a modal for each button clicked on, but with just on modal div in html.erb.我试图为点击的每个按钮创建一个模态,但只在 html.erb 中的模态 div 上。

For the modals to work they would need to be the same ID.要使模态工作,它们需要具有相同的 ID。

<% @services.each do |service| %>

<div class="card">
<a class="card-b" href="#" data-toggle="modal" data-target="# <%=service.name%> ">
</div>

<div class="modal" id=" <%=service.name%> " tabindex="-1" role="dialog" aria-hidden="true">
Hello this is the modal.
</div>

<% end %>

I would like to use rails code in the ID tag, I don't know if that is possible?我想在ID标签中使用rails代码,不知道是否可以?

EDIT:编辑:

I tried the best answer and it worked only for a few of the modals and the rest didn't.我尝试了最好的答案,它只对少数模态有效,其余的则无效。 Then I added a modal_tag column to my table and all the modals worked.然后我在我的表中添加了一个modal_tag列,所有的模态都起作用了。 Thank you!谢谢!

My code is now id="<%=service.modal_tag%>我的代码现在是id="<%=service.modal_tag%>

My preference is to use a unique value like SecureRandom.hex or __id__ as engineersmnky pointed out, instead of using model ids.我的偏好是使用像engineersmnky指出的SecureRandom.hex__id__这样的唯一值,而不是使用模型 ID。 Code would look something like this:代码看起来像这样:

# You may also have to require 'securerandom' within your rails app 
# (can't remember if that comes in automatically or not)
<% @services.each do |service| %>
  <!-- Either option for random_id works below -->
  <% random_id = SecureRandom.hex %> 
  <% random_id = service.__id__ %>

  <div class="card">
    <a class="card-b" href="#" data-toggle="modal" data-target="#<%= random_id %>">
  </div>

  <div class="modal" id="<%= random_id %>" tabindex="-1" role="dialog" aria-  hidden="true">
    Hello this is the modal.
  </div>
<% end %>

The reason I prefer this, is it doesn't leak database ids (if that's something you care about) and you're less likely to get id collisions in the event that this modal gets embedded into other pages.我更喜欢这个的原因是它不会泄漏数据库 ID(如果这是您关心的事情),并且在此模式嵌入其他页面的情况下,您不太可能发生 id 冲突。


And given a simple benchmarking script, it looks like __id__ is the more performant of the two options:给出一个简单的基准测试脚本,看起来__id__是两个选项中性能更高的:

$ cat unique_id.test.rb
require 'securerandom'
require 'benchmark'

n = 100_000
Benchmark.bm do |x|
  x.report { n.times do |n|  ; SecureRandom.hex; end }
  x.report { n.times do |n|  ; n.__id__ ; end }
end

$ ruby unique_id.test.rb
   user     system      total        real
   0.180000   0.000000   0.180000 (  0.176293)
   0.000000   0.000000   0.000000 (  0.005554)

Ruby on Rails has a dom_id method to generate unique ids for the use in HTML documents. Ruby on Rails 有一个dom_id方法来生成唯一的 id,以便在 HTML 文档中使用。 Furthermore, I do not like to mix HTML tags and ERB – therefore I would probably work with a helper method此外,我不喜欢混合 HTML 标签和 ERB——因此我可能会使用辅助方法

# in a helper
def service_modal(service, &block)
  tag.div(class: 'card') do
    tag.a(class: 'card-b', data: {toggle: 'modal', target: dom_id(service) }, href: '#')
  end +

  tag.div(class: 'modal', dom_id(service), tabindex: 1, role: 'dialog', 'aria-hidden': 'true') do
    capture(&block)
  end
end

and use it like this并像这样使用它

# in the view
<% @services.each do |service| %>
  <%= service_modal(service) do %>
    Hello this is the modal.
  <% end %>
<% end %>

Also, You can use object_id .此外,您可以使用object_id

Rails: data-target="#<%= service.object_id %>"> Rails: data-target="#<%= service.object_id %>">

Benchmark:基准:

   user     system      total        real
0.071505   0.000187   0.071692 (  0.072017) #SecureRandom.hex
0.004685   0.000016   0.004701 (  0.004727) #__id__
0.004500   0.000001   0.004501 (  0.004503) #object_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM