如何在 Ruby on Rails 4 中为帖子生成唯一的随机 ID? [英] How to generate a unique random id for post in Ruby on Rails 4?

查看:40
本文介绍了如何在 Ruby on Rails 4 中为帖子生成唯一的随机 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的博客应用程序中的帖子生成唯一的帖子标识符.

I want to produce unique post identifier for posts in my blogging application.

目前我正在使用 SecureRandom.hex(10) 为我的博客站点生成唯一的帖子标识符,但我不确定 SecureRandom 为此目的是否安全.

Currently I am using SecureRandom.hex(10) for generating unique post identifier for my blogging site but I am not sure is it safe SecureRandom for this purpose.

有没有其他方法可以做到这一点?

Is there any other way to do this?

推荐答案

来自 Ruby 文档:

From the Ruby doc:

这个库是一个安全随机数生成器的接口,适用于在 HTTP cookie 等中生成会话密钥.

This library is an interface for secure random number generator which is suitable for generating session key in HTTP cookies, etc.

我遇到了类似的问题,我使用了 Digest 库.

I had similar problem, I used Digest library.

Digest::MD5.hexdigest(post.title + post.created_at.to_s) #=> "b4809d..."

这篇关于如何在 Ruby on Rails 4 中为帖子生成唯一的随机 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆