Rails:生成唯一的随机数以填充CSS类String如何?

时间:2021-12-17 20:34:46

So let's say we have a rails view with this code:

所以我们假设我们有一个使用此代码的rails视图:

<h1>Users who have the <%= @title.name %> Title</h1>
<ul>
  <% @title.users.each do |user| %>
    <li><%= user.username %> <div class="rw-ui-container rw-urid-X"></div></li>
  <% end %>
</ul>

And what we want to do is to automatically print this line:

我们想要做的是自动打印这一行:

<div class="rw-ui-container rw-urid-X"></div>

Right aside each username the loop throws at us BUT we want X to be a different unique random number each time...

除了每个用户名,循环抛出我们但是我们希望X每次都是一个不同的唯一随机数...

How can this be accomplished?

如何实现这一目标?

3 个解决方案

#1


1  

May be this solution can help you: 1. You can use user_id as first part of the random X to ensure that another user doesn't have it 2. Second part is time in UTC format to set X different each time 3. Third part is just a standart random func. You haven't use it if you want.

可能这个解决方案可以帮助你:1。你可以使用user_id作为随机X的第一部分,以确保另一个用户没有它2.第二部分是UTC格式的时间,每次设置X不同3.第三部分只是一个标准的随机功能。如果你愿意,你没有使用它。

<h1>Users who have the <%= @title.name %> Title</h1>
<ul>
  <% @title.users.each do |user| %>
    <li><%= user.username %> <div class="rw-ui-container rw-urid-<%= "#{user_id}#{Time.now.utc.to_i}#{rand(100000)}".to_i %>"></div></li>
  <% end %>
</ul>

#2


0  

css_arr = ["rw-ui-container rw-urid-1","rw-ui-container rw-urid-2","rw-ui-container rw-urid-3"]

<div class="<%= css_arr.sample %>"></div>

#3


0  

Well You could easily add a new field to user table call uuid or give it any name that you like and assign a unique id using the gem called uuid

那么你可以轻松地向用户表调用uuid添加一个新字段或给它任何你喜欢的名称,并使用名为uuid的gem分配一个唯一的id

This help ensure that you have uniqueness in X section and also on refresh the value wont change as you mention in one of your comment

这有助于确保您在X部分中具有唯一性,并且还可以刷新您在其中一条评论中提及的值

so here how your X code would look like

所以这里你的X代码看起来如何

<% @title.users.each do |user| %>
    <li><%= user.username %> <div class="rw-ui-container rw-urid-<%=user.uuid %>"></div></li>
  <% end %>

#1


1  

May be this solution can help you: 1. You can use user_id as first part of the random X to ensure that another user doesn't have it 2. Second part is time in UTC format to set X different each time 3. Third part is just a standart random func. You haven't use it if you want.

可能这个解决方案可以帮助你:1。你可以使用user_id作为随机X的第一部分,以确保另一个用户没有它2.第二部分是UTC格式的时间,每次设置X不同3.第三部分只是一个标准的随机功能。如果你愿意,你没有使用它。

<h1>Users who have the <%= @title.name %> Title</h1>
<ul>
  <% @title.users.each do |user| %>
    <li><%= user.username %> <div class="rw-ui-container rw-urid-<%= "#{user_id}#{Time.now.utc.to_i}#{rand(100000)}".to_i %>"></div></li>
  <% end %>
</ul>

#2


0  

css_arr = ["rw-ui-container rw-urid-1","rw-ui-container rw-urid-2","rw-ui-container rw-urid-3"]

<div class="<%= css_arr.sample %>"></div>

#3


0  

Well You could easily add a new field to user table call uuid or give it any name that you like and assign a unique id using the gem called uuid

那么你可以轻松地向用户表调用uuid添加一个新字段或给它任何你喜欢的名称,并使用名为uuid的gem分配一个唯一的id

This help ensure that you have uniqueness in X section and also on refresh the value wont change as you mention in one of your comment

这有助于确保您在X部分中具有唯一性,并且还可以刷新您在其中一条评论中提及的值

so here how your X code would look like

所以这里你的X代码看起来如何

<% @title.users.each do |user| %>
    <li><%= user.username %> <div class="rw-ui-container rw-urid-<%=user.uuid %>"></div></li>
  <% end %>