Permalink
Browse files

Add unfollow user button

  • Loading branch information...
1 parent 2724b22 commit 29fa67a5ec3fb3593f10062d5a16c2dc9f488637 @anthonylewis anthonylewis committed Mar 14, 2015
Showing with 26 additions and 2 deletions.
  1. +10 −0 app/controllers/users_controller.rb
  2. +6 −0 app/models/user.rb
  3. +9 −2 app/views/users/show.html.erb
  4. +1 −0 config/routes.rb
@@ -28,6 +28,16 @@ def follow
end
end
+ def unfollow
+ @user = User.find(params[:id])
+
+ if current_user.unfollow!(@user)
+ redirect_to @user, notice: "Unfollow successful!"
+ else
+ redirect_to @user, alert: "Error unfollowing."
+ end
+ end
+
private
def user_params
View
@@ -31,6 +31,12 @@ def follow!(leader)
end
end
+ def unfollow!(leader)
+ if leader != self && following?(leader)
+ leaders.delete(leader)
+ end
+ end
+
def timeline_user_ids
leader_ids + [id]
end
@@ -8,8 +8,15 @@
<p class="lead">API Token: <%= @user.api_token %></p>
<% end %>
-<%= link_to "Follow", follow_user_path(@user),
- method: :post, class: "btn btn-default" %>
+<% if current_user && current_user != @user %>
+ <% if current_user.following?(@user) %>
+ <%= link_to "Unfollow", unfollow_user_path(@user),
+ method: :delete, class: "btn btn-default" %>
+ <% else %>
+ <%= link_to "Follow", follow_user_path(@user),
+ method: :post, class: "btn btn-default" %>
+ <% end %>
+<% end %>
<h2>Posts</h2>
View
@@ -8,6 +8,7 @@
get 'signup', to: 'users#new', as: 'signup'
post 'follow/:id', to: 'users#follow', as: 'follow_user'
+ delete 'unfollow/:id', to: 'users#unfollow', as: 'unfollow_user'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'

0 comments on commit 29fa67a

Please sign in to comment.