Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want a user to be able to follow a topic(tag) that is created with acts_as_taggable_on in Rails 4

I have a question model much like stackoverflow.

A user can add a question. In the question form there is :Title, :topics, :place, :description

Each topic has its own show page which has a feed of questions that contain the topic.

I want to add a feature so that when a user is on the show page of the topic there is a follow button and the user will be able to follow the topic.

I'm not sure if acts_as_taggable has an in built feature to do so? I am using acts_as_follower so users can follow each other and this is all working fine!

I was going to try to use acts_as_follower on the topics but I have no idea how since there is no 'topic' model as it is created via acts_as_taggable_on

topic_controller.rb for show page

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

Any ideas and code will help alot,

Thanks!

like image 380
Lewis Frost Avatar asked Jan 15 '14 12:01

Lewis Frost


1 Answers

You can create a Topic model that inherits from ActsAsTaggableOn::Tag, and add acts_as_followable to that model.

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

Then, you can use topic as your tag:

# app/models/post.rb
class Post < ActiveRecord::Base
  acts_as_taggable_on :topics
end
like image 89
Anand Avatar answered Oct 25 '22 21:10

Anand