简体   繁体   English

如何在Rails中生成随机字符串

[英]How to generate a random string in rails

In my form i want to generate random strings for the following fields 在我的表格中,我想为以下字段生成随机字符串

  f.input :lti_api_key, label: "LTI API KEY"
    f.input :lti_api_secret, label: "LTI API SECRET"

how can i do that? 我怎样才能做到这一点? Please help me... 请帮我...

您可以这样输入f.input

<%= f.text_field :name, id: :bug_name, value: "#{SecureRandom.hex(64)}" %>

Hello you can do that: 您好,您可以这样做:

require 'securerandom'
SecureRandom.hex(64)

Take a look at the string_pattern gem project: https://github.com/MarioRuiz/string_pattern 看一下string_pattern gem项目: https : //github.com/MarioRuiz/string_pattern

To install it: gem install string_pattern 要安装它: gem install string_pattern

This is an example of use: 这是一个使用示例:

require 'string_pattern'
# four characters. optional: capitals and numbers, required: lower
"4:XN/x/".gen    # aaaa, FF9b, j4em, asdf, ADFt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM