-
-
Save tcocca/872858 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>'); | |
#jQuery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>'); | |
#jQuery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
resources :users, :only => [:index, :show] do | |
resources :follows, :only => [:create, :destroy] | |
end | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% if user_signed_in? %> | |
<div id="follow_user"> | |
<%= render :partial => "shared/follow_user", :locals => {:user => @user} %> | |
</div> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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