Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何将rails代码添加到div标记的id属性中?_Ruby On Rails_Ruby_Bootstrap 4 - Fatal编程技术网

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

Ruby on rails 如何将rails代码添加到div标记的id属性中?,ruby-on-rails,ruby,bootstrap-4,Ruby On Rails,Ruby,Bootstrap 4,我想这样做:我的首选是使用一个唯一的值,如或所指出的,而不是使用模型ID。代码如下所示: # 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 w

我想这样做:
我的首选是使用一个唯一的值,如或所指出的,而不是使用模型ID。代码如下所示:

# 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 %>

此外,您还可以使用
对象\u id

Rails:
数据目标=“#”>

基准:

   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
RubyonRails有一个生成HTML文档中使用的唯一ID的方法。此外,我不喜欢混合使用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
像这样使用它

# in the view
<% @services.each do |service| %>
  <%= service_modal(service) do %>
    Hello this is the modal.
  <% end %>
<% end %>
视图中的
#
你好,我是莫代尔。

删除
数据目标中的空格
id
。同样,对于
id
而言,名称必须是唯一的,否则如下所述,使用数据库
id
(保证是唯一的)。还要确保关闭锚定标记“当在HTML元素上指定时,id属性值在元素树中的所有id中必须是唯一的,并且必须至少包含一个字符。该值不得包含任何空格字符。”-那么为什么要生成一个
hex
<代码>\u服务。\uuuuu id\uuuuu
不会泄漏数据库id,并且可能是唯一的,因为我怀疑
服务
是一个不可变的internedobject@engineersmnky从未想过使用基础对象id。根据文档:
没有两个活动对象将共享一个id
,因此它也可以正常工作。HTML id必须以信,就让你知道。但是,您可以很容易地在生成的id中添加某种前缀,例如“service-”。@Nate不正确。根据“id”属性指定其元素的唯一标识符(id)。对于ID的格式没有其他限制;特别是,ID可以只包含数字、以数字开头、以下划线开头、仅包含标点符号等。”@engineersmnky这仅在HTML5中是正确的。在此之前,我的说法是正确的。OP没有指定他们使用的是HTML4还是HTML5,所以我发表了评论。此外,一些文章会建议您仍然以字母开头,以确保向后兼容性。此外,在ID中添加前缀将有助于防止ID冲突,OP担心的是alr已经准备好了。基准测试可能有点误导,因为
\uu id\uu
object\u id
是同一个内部callI的别名。我完全不知道
dom\u id
方法。很高兴学习一个新技巧。TIL!这也是一个很好的解决方案。它会泄漏数据库标识符,但对于大多数用例来说是最合适的。@GavinMil这是不正确的。但是这不应该是一个问题,因为Rails每默认都会在URL中泄漏数据库标识符。如果你使用一个类似GooRyLyId的ID或者覆盖ToPARAM方法来避免这种行为,那么你也应该考虑重写ToKyKy。
# 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
# in the view
<% @services.each do |service| %>
  <%= service_modal(service) do %>
    Hello this is the modal.
  <% end %>
<% end %>