【问题标题】:How do I make ID a random 8-digit alphanumeric in Rails 4?如何在 Rails 4 中将 ID 设为随机的 8 位字母数字?
【发布时间】:2013-09-03 17:53:21
【问题描述】:

我正在构建一个简单的应用程序,访问者可以在其中创建/编辑帖子并获得指向他们帖子的永久链接,但我希望链接更长且更难猜。

我发现“how to make ID a random 8 digit alphanumeric in rails?”可以在我的网站上执行我想要执行的操作,但它在 rails 4 上不起作用,因为“attr_accessible”不再可用。

【问题讨论】:

  • 抱歉,我再次阅读了您的问题,但看起来无法接受。我投票关闭它。您收到此错误是因为 Rails 4 不使用 attr_accessible。只需从模型中删除该行。但是,您的问题无效。
  • @fotanus 你能说为什么这是不可接受的吗?这个问题似乎有效。之前有没有被问过,发帖人没有尽职尽责先找到答案?也许可以改写或扩展该问题以询问如何处理 attr_accessible 错误?
  • 我因此关闭:Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. [See also: Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist) 但我也认为此限制也适用:There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.
  • @fotanus 很明显,我厌倦了帖子中链接中的内容,我确实提到错误是因为 attr_accessible 在 rails 4 中不再可用。我找不到任何类似的示例我为rails 4链接的那个如果我这样做了,那么我就不会问这个问题了。如果它是一个无效的问题,请关闭或删除。

标签: ruby-on-rails ruby random permalinks


【解决方案1】:

使用SecureRandom?试试这个:

SecureRandom.hex[0..7]

【讨论】:

  • 仍然是 Rails 的初学者,不知道如何使用它。
猜你喜欢
  • 2013-04-12
  • 2015-07-29
  • 2019-09-08
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2014-09-08
相关资源
最近更新 更多