Skip to content

Instantly share code, notes, and snippets.

@tcocca
Forked from gaaady/FollowsController.rb
Created March 16, 2011 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcocca/872858 to your computer and use it in GitHub Desktop.
Save tcocca/872858 to your computer and use it in GitHub Desktop.
<% 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
@followable = find_followable
@follower = find_follower
@follower.follow(@followable)
redirect_to (@followable)
end
def destroy
@followable = find_followable
@follower = find_follower
@follower.stop_following(@followable)
redirect_to (@followable)
end
private
def find_followable
params.each do |name, value|
if name =~ /(.+)_id$/ && name != 'follower_id'
return $1.classify.constantize.find(value)
end
end
nil
end
def find_follower
if !params[:follower_id].blank? && !params[:follower_type].blank?
params[:follower_type].classify.constantize.find(params[:follower_id])
else
current_user
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment