Skip to content

Instantly share code, notes, and snippets.

@tcocca
Created March 9, 2011 21:55
Show Gist options
  • Save tcocca/863091 to your computer and use it in GitHub Desktop.
Save tcocca/863091 to your computer and use it in GitHub Desktop.
sample of how to use acts_as_follower with rails3 and ajax
<% unless user == current_user %>
<% if current_user.following?(user) %>
<%= button_to("Un-Follow #{user.nickname}", user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true) %>
<% else %>
<%= button_to("Follow #{user.nickname}", user_follows_path(user.to_param), :remote => true) %>
<% end %>
<% end %>
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');
#jQuery
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');
#jQuery
class FollowsController < ApplicationController
def create
@user = User.find(params[:user_id])
current_user.follow(@user)
end
def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end
...
resources :users, :only => [:index, :show] do
resources :follows, :only => [:create, :destroy]
end
...
<% if user_signed_in? %>
<div id="follow_user">
<%= render :partial => "shared/follow_user", :locals => {:user => @user} %>
</div>
<% end %>
class User < ActiveRecord::Base
...
acts_as_follower
acts_as_followable
...
end
@amritdeep
Copy link

If we want implement it with mongodb then How can we do it ?

@RailsCod3rFuture
Copy link

Can you update this solution for Rails 4 please?

@tserinken
Copy link

Does it work with MongoDB?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment