Generating unique token on the fly with Rails

前端 未结 6 1449
醉话见心
醉话见心 2021-01-31 18:50

I want to generate a token in my controller for a user in the \"user_info_token\" column. However, I want to check that no user currently has that token. Would this code suffice

6条回答
  •  时光说笑
    2021-01-31 19:47

    Rails 5 comes with this feature, you only need to add to your model the next line:

    class User
      has_secure_token
    end
    

    Since Rails 5 is not releases yet you can use the has_secure_token gem. Also you can see my blog post to see more info about it https://coderwall.com/p/kb97gg/secure-tokens-from-rails-5-to-rails-4-x-and-3-x

提交回复
热议问题