Rails 3:确定用户是否已查看数据 [英] Rails 3: Determine if a user has viewed data

查看:75
本文介绍了Rails 3:确定用户是否已查看数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户以前是否查看过与该控制器关联的记录来修改控制器index.html.erb页面的内容.

I'd like to modify the contents of a controller's index.html.erb page based on whether or not a user has previously viewed records associated with that controller.

在我的特定情况下,我有一个Item模型,Items控制器和一个Items视图.项目的视图index.html.erb显示每个项目的链接.我需要设置此视图,以便如果以前查看过某个项目,则它在index.html.erb上的链接将以斜体显​​示.如果未查看,则该链接将为粗体.

In my particular case, I have an Item model, Items controller, and an Items view. The Item's view index.html.erb displays a link to each Item. I need to setup this view such that if an item has been viewed previously, then its link on index.html.erb would be in italics. If it had not been viewed, the link would be in boldface.

从视觉上看,这种方法类似于电子邮件收件箱,其中未查看的项目的主题标题以粗体显示,已查看的项目具有常规的字体粗细.

Visually, this approach is similar to an e-mail inbox, where items that have not been viewed have subject headings listed in bold face and viewed items have a regular font weight.

我的问题类似于此先前的堆栈溢出发布;但是,该帖子提供了有关数据库结构的一般答案.我很想知道是否有一种轨道方法"来实现我所缺少的这种行为.如果链接的帖子合适,那么谁能提出建议,说明如何使用Rails实现帖子的推荐解决方案?

My question is similar to this previous stack overflow post; however, that post provides a general answer related to database structure. I'm curious to know if there is a ''Rails way'' to achieve this behavior that I am missing. If the linked post is appropriate, can anyone offer suggestions as how to achieve the posts' recommended solution using Rails?

我可以依靠浏览器并使用适当的CSS来设置访问和未访问的URL的样式,但是由于与我的项目相关的其他原因(但实际上不是这个问题),我宁愿有一个依赖于Rails,钩子的解决方案和/或数据库.如果没有直接的Rails方式,我也将寻求其他解决方案(例如,基于jQuery的解决方案).

I could rely on the browser and use the appropriate CSS to style visited and unvisited URLs, but for other reasons relevant to my project (but not this question, really) I'd rather have a solution that relies on Rails, hooks, and/or the database. I'm also up for other solutions (e.g., jQuery-based) if there's no straightforward Rails way of doing this.

推荐答案

老实说,这似乎是View的模型,该模型具有一个item_id和一个user_id.我想说的最重要的是制作View资源.

Honestly, it seems like option 1 of the link you mentioned is the most "Rails way" of doing it. You make a model called a View, which has an item_id and a user_id. I'd say the most important thing is making a View resource.

更新:因此,假设您想将某个项目标记为已查看(如果用户已访问该项目的Items#show).这很容易-在Items#show中,在呈现该页面的视图之前,请执行以下操作:

Update: So let's say you want to mark an item as viewed if the user has accessed Items#show for that item. It's easy enough -- in Items#show, before you render that page's view, do something like this:

View.create(item_id: params[:id], user_id: current_user.id)

如果您担心current_user无法登录或类似问题,可以在Items#show上创建before_filter来登录用户(如果您要求用户在查看用户之前登录,项)或仅有条件地创建view:

If you're worried about current_user not being logged in or something like that, you can either make a before_filter on Items#show to log the user in (if you require that he be logged before he views an item) OR just create the view conditionally:

if current_user
  View.create(item_id: params[:id], user_id: current_user.id)
end

这篇关于Rails 3:确定用户是否已查看数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆