public
Last active

sample of how to use acts_as_follower with rails3 and ajax

  • Download Gist
FollowsController.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
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
_follow_user.html.erb
HTML+ERB
1 2 3 4 5 6 7
<% 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 %>
create.js.erb
HTML+ERB
1 2 3
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');
 
#jQuery
destroy.js.erb
HTML+ERB
1 2 3
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');
 
#jQuery
routes.rb
Ruby
1 2 3 4 5 6 7
...
 
resources :users, :only => [:index, :show] do
resources :follows, :only => [:create, :destroy]
end
 
...
show.html.erb
HTML+ERB
1 2 3 4 5
<% if user_signed_in? %>
<div id="follow_user">
<%= render :partial => "shared/follow_user", :locals => {:user => @user} %>
</div>
<% end %>
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10
class User < ActiveRecord::Base
...
 
acts_as_follower
acts_as_followable
...
end

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.