开发者

How do I save temporary data for anonymous users in Rails

开发者 https://www.devze.com 2023-02-01 05:57 出处:网络
For example, if an anonymous user clicks on \"like\" button, he will 开发者_StackOverflow社区be redirected to a signin page; the user can choose either to sign up or sign in, in either way server will

For example, if an anonymous user clicks on "like" button, he will 开发者_StackOverflow社区be redirected to a signin page; the user can choose either to sign up or sign in, in either way server will remember his action so user does not to click again. The data should be destroyed 10 minutes after it's created.

How do I implement this?


You basically just want to serialize a list of things to be done once they create an account. You could store this in a session, a persistent cache like memcached (though you aren't guaranteed persistence), or a custom model/table. There's not really any single accepted way to do this in Rails, so it's left up to you as to how you implement it.

What you could do, if you want to stick to the Rails way of doing things, is to set a transient flag on a record, then save it. Once the user registers, you can remove the transient flag. Run a cron job that destroys all transient records older than 10 minutes, and all your before_destroy callbacks should be run to let you roll back any state changes they caused. That's a "heavy" solution, but it's easy to implement and lets you leverage Rails' models easily.


I would use sessions to store the temporary data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号