Skip to content

Instantly share code, notes, and snippets.

@tagrudev
Last active December 16, 2015 03:39
Show Gist options
  • Save tagrudev/5371199 to your computer and use it in GitHub Desktop.
Save tagrudev/5371199 to your computer and use it in GitHub Desktop.
Followers/Following
$(document).ready ->
$("#following_option").on "ajax:complete", (evn, data) ->
$("#following_option").html(data.responseText)
// users/index.html.haml
#following_option
- if current_user.follower_of? user
= render "users/unfollow", user: user
- else
= render "users/follow", user: user
// users/_follow.html.haml
= button_to user_followees_path(user), remote: true, class: "btn", form_class: "following_option" do
%i.icon-user Follow
// users/_unfollow.html.haml
= button_to user_followee_path(id: current_user, user_id: user.id), remote: true, method: :delete, class: "btn", form_class: "following_option" do
%i.icon-user Unfollow
// followees controller
def create
current_user.follow @model
current_user.save
@model.save
if request.xhr?
return render partial: 'users/unfollow', locals: { user: @model }
end
end
def destroy
user = User.where(id: params[:user_id]).first
current_user.unfollow user
current_user.save
user.save
if request.xhr?
return render partial: 'users/follow', locals: { user: @model }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment