Rails:生成唯一的随机数以填充CSS类String如何? [英] Rails: Generate Unique Random Number for populating a CSS class String How To?
问题描述
因此,我们可以使用以下代码查看Rails:
So let's say we have a rails view with this code:
<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>
我们要做的是自动 print 这行:
And what we want to do is to automatically print this line:
<div class="rw-ui-container rw-urid-X"></div>
在每个用户名旁边,循环会向我们抛出,但我们希望 X 每次都是一个不同的 unique 随机数...
Right aside each username the loop throws at us BUT we want X to be a different unique random number each time...
这如何实现?
推荐答案
也许此解决方案可以为您提供帮助:
1.您可以将 user_id
用作第一部分。随机 X
来确保另一个用户没有
2.第二部分是以UTC格式设置的时间X
每次
都不同。3.第三部分只是一个标准的随机函数。
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.
<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>
这篇关于Rails:生成唯一的随机数以填充CSS类String如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!