Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created August 25, 2016 22:38
Show Gist options
  • Save kenmazaika/0f43614415c66de2e900fe6611391d3e to your computer and use it in GitHub Desktop.
Save kenmazaika/0f43614415c66de2e900fe6611391d3e to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
acts_as_followable
acts_as_follower
end
User.create(email: "[email protected]", password: "secretpassword123", password_confirmation: "secretpassword123")
User.create(email: "[email protected]", password: "secretpassword123", password_confirmation: "secretpassword123")
ken = User.first
marco = User.last
marco.follow!(ken)
ken.followers(User) # marco
marco.followees(User) # ken
# POST /users/:user_id/followers
config/routes.rb
resources :users, only: [] do
resources :followers, only: [:create]
end
class FollowersController < ActionController::Base
def create
other_user = User.find(params[:user_id])
current_user # two different users we can access
current_user.follow!(other_user)
redirect_to root_path, notice: "You're following them now"
end
def create
other_user = User.find(params[:user_id])
current_user.follow!(other_user)
redirect_to root_path, notice: "You're following them now"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment