在 Rails 3 应用程序中,如何允许匿名用户仅访问控制器操作一次?

发布于 2024-12-08 18:10:41 字数 358 浏览 6 评论 0原文

例如,假设我有一个博客,任何人都可以阅读文章、阅读评论并将任何评论标记为不当。如何防止未登录的用户多次点击“举报评论”链接?

“标记注释”链接将直接绑定到注释模型的控制器方法。

我对会话和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-12-15 18:10:41

基本思想如下(抱歉,还没有代码):

  1. 定义哪些信息应存储在会话和/或 cookie 中。我认为它应该是每个标记评论的评论id。将它们存储在类似散列的结构中。
  2. 根据 cookie 的内容创建指向标记评论的链接。类似这样的事情:

    <前><代码>...
    = link_to('标记评论', flag_comment_path(comment.id)) if ! cookie[:标记评论] || ! cookies[:flaged_comments][comment.id]

  3. 标记评论时设置cookie哈希值(此处使用本地变量comment,必须在某处设置或知道该值):

    cookies[:flaagged_comments] = Hash.new 如果! cookie[:标记的评论]
    cookies[:flaged_comments][comment.id] = comment.id
    

我不知道是否代码可以工作,但想法应该清晰。是的,仅对匿名用户执行此操作(更依赖 UI 和控制器功能)。

还有一件事:我认为您不应该使用会话和 cookie 来存储此信息。由于您必须注意到有人在两个不同的会话中标记评论,因此仅使用 cookie。

The basic idea could be the following (sorry, no code yet):

  1. Define which information should be stored in a session and / or a cookie. I think it should be the id of the comment for each flagged comment. Store them in a hash like structure.
  2. Make the link to flagging a comment depending on the content of the cookie. Something like that:

    ...
    = link_to('flag comment', flag_comment_path(comment.id)) if ! cookies[:flagged_comments] || ! cookies[:flagged_comments][comment.id]
    
  3. Set the cookies hash value when a comment is flagged (use here the local variable comment, this has to be set or known somewhere):

    cookies[:flagged_comments] = Hash.new if ! cookies[:flagged_comments]
    cookies[:flagged_comments][comment.id] = comment.id
    

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文