【问题标题】:I want a user to be able to follow a topic(tag) that is created with acts_as_taggable_on in Rails 4我希望用户能够关注在 Rails 4 中使用acts_as_taggable 创建的主题(标签)
【发布时间】:2014-01-15 12:53:04
【问题描述】:

我有一个很像 stackoverflow 的问题模型。

用户可以添加问题。在问题形式中有 :Title, :topics, :place, :description

每个主题都有自己的显示页面,其中包含包含该主题的问题提要。

我想添加一个功能,以便当用户在主题的显示页面上时有一个关注按钮,用户将能够关注该主题。

我不确定acts_as_taggable 是否具有这样做的内置功能?我正在使用acts_as_follower,所以用户可以互相关注,这一切都很好!

我打算尝试在主题上使用acts_as_follower,但我不知道如何,因为没有“主题”模型,因为它是通过acts_as_taggable_on 创建的

显示页面的topic_controller.rb

class TopicController < ApplicationController
  def index
    @questions = Question.tagged_with(params[:topic])

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @questions }
      end
  end
end

任何想法和代码都会有很大帮助,

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 acts-as-taggable-on


    【解决方案1】:

    您可以创建一个继承自 ActsAsTaggableOn::Tag 的 Topic 模型,并将acts_as_followable 添加到该模型中。

    # app/models/topic.rb
    class Topic < ActsAsTaggableOn::Tag
      acts_as_followable
    end
    

    然后,您可以使用主题作为您的标签:

    # app/models/post.rb
    class Post < ActiveRecord::Base
      acts_as_taggable_on :topics
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多