Skip to content

Instantly share code, notes, and snippets.

@gaaady
Forked from tcocca/FollowsController.rb
Created March 16, 2011 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gaaady/872727 to your computer and use it in GitHub Desktop.
Save gaaady/872727 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
current_user.follow(@followable)
redirect_to (@followable)
end
def destroy
@followable = find_followable
current_user.stop_following(@followable)
redirect_to (@followable)
end
private
def find_followable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
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