简体   繁体   English

使用Ruby on Rails进行标签过滤

[英]Hashtag filtering with Ruby on Rails

New Rails programmer here. 这里是新的Rails程序员。 There is probably a pretty simple solution to this, but I just can't figure it out. 可能有一个非常简单的解决方案,但是我无法弄清楚。 Here's the deal: lets say I have many posts on a single page. 这是交易:可以说我在一个页面上有很多帖子。 Each post has a content field, and some of those content fields contain #hashtags within the content. 每个帖子都有一个内容字段,其中一些内容字段在内容中包含#hashtags。

I want to develop an algorithm that scans the content of every single post (say, every 2 seconds) and displays a list of every hashtag. 我想开发一种算法,可扫描每个帖子(例如,每2秒)的内容并显示每个主题标签的列表。 I'm thinking AJAX will be necessary, because the posts feature AJAX edit-in-place. 我认为AJAX是必要的,因为这些帖子都具有就地AJAX编辑功能。 Thus, if a post is changed and a new hashtag is created, the hashtag list should be updated automatically to reflect this. 因此,如果更改了帖子并创建了新的主题标签,则主题标签列表应自动更新以反映这一点。 Also, each hashtag on the hashtag list should be clickable, sending a search query and in turn displaying the posts that contain the selected hashtag. 同样,主题标签列表上的每个主题标签都应是可单击的,从而发送搜索查询并依次显示包含所选主题标签的帖子。

This end result is similar to tagging and a tag cloud, but should be dynamic and integrated (as opposed to having a tag field for each post). 最终结果类似于标记和标签云,但是应该是动态的和集成的(与每个帖子都有一个标签字段相对)。 I don't want to attach any tags to the posts as a database column. 我不想将任何标签作为数据库列附加到帖子。 I just want the application to scan the content of every single post and display a clickable hashtag list. 我只希望应用程序扫描每个帖子的内容并显示可点击的标签列表。

All suggestions are greatly appreciated. 所有建议,不胜感激。

Personally, I would use jQuery to implement the AJAX functionality you need. 就个人而言,我将使用jQuery来实现所需的AJAX功能。 Javascript will be used to check the server for new hashtags, and the actual hashtag searching will be done on the server. Javascript将用于检查服务器上是否有新的标签,并且实际的标签搜索将在服务器上完成。 In your Post model, you could have something like this to do the actual searching: 在您的Post模型中,您可能需要执行以下操作来进行实际搜索:

class Post < ActiveRecord::Base
  # class methods
  class << self
    def hashtags
      tags = []

      # cycle through all posts that contain a hashtag, and add them to list
      Post.all(:conditions => 'body like "%#"').each do |post|
        tags += post.hashtags
      end

      # remove duplicates and sort alphabetically
      tags = tags.uniq.sort
    end
  end

  # instance methods
  def hashtags
    @hashtags ||= body.scan(/#\w+/)
  end
end

There are two methods here with the same name, but one is called on the class ( Post.hashtags ) to get all hashtags across all posts, and the second one is called on a single instance ( post.hashtags ) to get just the hashtags from that post. 这里有两种名称相同的方法,但是在类( Post.hashtags )上调用一种方法以获取所有帖子中的所有主题标签,而在单个实例( post.hashtags )上调用post.hashtags以仅获取主题标签。从那个帖子。

There are several other pieces to the puzzle - your controller code, your views, and your javascript. 这个难题还有其他几个方面-您的控制器代码,您的视图和您的JavaScript。 This question is a tall order for a volunteer site. 这个问题对于志愿人员来说是一个很高的要求。 You're asking for several different things to be done for you. 您正在要求为您做几件不同的事情。

My advice is to start building this as I've described, and ask for help on the pieces that you have trouble with along the way. 我的建议是按照我的描述开始构建它,并在过程中遇到问题时寻求帮助。 We're more than happy to help, but you're asking for an entire javascript-driven MVC stack. 我们非常乐意为您提供帮助,但您需要的是整个javascript驱动的MVC堆栈。 Try to limit your questions to something that can be answered in 5-10 minutes, and you're likely to get a lot more feedback and help. 尝试将您的问题限制在5-10分钟内可以回答的问题上,您可能会获得更多的反馈和帮助。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM