Use a random string as id in Ruby on Rails?

后端 未结 4 718
暖寄归人
暖寄归人 2021-02-06 18:45

I want to create a web app similar to http://www.pastebin.com/ in Ruby on Rails. pastebin.com uses a random string to identify an item. Ruby on Rails uses an auto-incrementing n

4条回答
  •  心在旅途
    2021-02-06 19:17

    For vanilla ruby

    require 'securerandom'
    require 'base64'
    
    slug = Base64.encode64(SecureRandom.uuid)[0..10]
    => "YWVkNzZmYjI" 
    => "MzQxMDkxY2U"
    

提交回复
热议问题