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

时间:2017-07-05 14:02:34

标签: ruby-on-rails ruby-on-rails-4

在我的表单中,我想为以下字段生成随机字符串

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

我该怎么做? 请帮帮我......

3 个答案:

答案 0 :(得分:3)

您可以将f.input视为

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

答案 1 :(得分:1)

你好,你可以这样做:

require 'securerandom'
SecureRandom.hex(64)

答案 2 :(得分:-1)

看看string_pattern gem项目:https://github.com/MarioRuiz/string_pattern

要安装它:gem install string_pattern

这是一个使用示例:

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