在 Rails 3 应用程序中,如何允许匿名用户仅访问控制器操作一次?
例如,假设我有一个博客,任何人都可以阅读文章、阅读评论并将任何评论标记为不当。如何防止未登录的用户多次点击“举报评论”链接?
“标记注释”链接将直接绑定到注释模型的控制器方法。
我对会话和 cookie(以及一般的 Rails)的想法很陌生。我已经在 Sessions 上读过这篇文章,但恐怕我还是有点困惑。
我考虑过创建一个名为 Guest 的基类,但我想知道是否可以避免这种情况,而是利用会话或 cookies 临时数据。
提前致谢。
For example, suppose I had a blog and anybody could read the articles, read the comments, and flag any comment as inappropriate. How do I prevent non-signed-in users from clicking the "Flag Comment" link more than once?
The "Flag Comment" link would be tied directly to a controller method for a Comment model.
I'm new to the idea of sessions and cookies (as well as Rails in general). I've read this on Sessions but I'm afraid I'm still a little confused.
I've considered creating a Base class called Guest, but I was wondering if I could avoid this and instead utilize session or cookies temp data.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本思想如下(抱歉,还没有代码):
评论
的id
。将它们存储在类似散列的结构中。根据 cookie 的内容创建指向
标记评论
的链接。类似这样的事情:<前><代码>...
= link_to('标记评论', flag_comment_path(comment.id)) if ! cookie[:标记评论] || ! cookies[:flaged_comments][comment.id]
标记评论时设置cookie哈希值(此处使用本地变量
comment
,必须在某处设置或知道该值):我不知道是否代码可以工作,但想法应该清晰。是的,仅对匿名用户执行此操作(更依赖 UI 和控制器功能)。
还有一件事:我认为您不应该使用会话和 cookie 来存储此信息。由于您必须注意到有人在两个不同的会话中标记评论,因此仅使用 cookie。
The basic idea could be the following (sorry, no code yet):
id
of thecomment
for each flagged comment. Store them in a hash like structure.Make the link to
flagging a comment
depending on the content of the cookie. Something like that:Set the cookies hash value when a comment is flagged (use here the local variable
comment
, this has to be set or known somewhere):I don't know if the code will work, but the idea should be clear. And yes, do that only to anonymous users (more dependent UI and controller functionality).
One more thing: I don't think you should use the session and the cookies for storing this information. And due to the fact that you have to notice when someone flags a comment in 2 different sessions, go with the cookies only.