Skip to content

Loading…

add simple following api. #30

Merged
merged 1 commit into from

2 participants

@ledbettj
Collaborator

per your request.

To follow a user:

$.post('/follow/1.json');

To unfollow:

$.ajax('/follow/1.json', {type: 'delete'});
@cjstewart88 cjstewart88 merged commit 479062a into cjstewart88:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 6, 2013
  1. @ledbettj

    add simple following api.

    ledbettj committed
Showing with 27 additions and 0 deletions.
  1. +24 −0 app/controllers/follows_controller.rb
  2. +3 −0 config/routes.rb
View
24 app/controllers/follows_controller.rb
@@ -0,0 +1,24 @@
+class FollowsController < ApplicationController
+ before_filter :validate_follow_params
+
+ def follow
+ current_user.follow(@who)
+ head 201
+ end
+
+ def unfollow
+ current_user.stop_following(@who)
+ head 204
+ end
+
+ private
+
+ def validate_follow_params
+ head 403 and return unless user_signed_in?
+
+ @who = User.find(params[:who])
+ rescue ActiveRecord::RecordNotFound
+ render :status => 400, :json => {error: "User not found"}
+ end
+
+end
View
3 config/routes.rb
@@ -16,6 +16,9 @@
get "/r" => "application#explore"
get "/explore" => "application#explore"
+ post "/follow/:who" => "follows#follow"
+ delete "/follow/:who" => "follows#unfollow"
+
get "/users" => "users#list"
get "/:username/profile" => "users#profile"
get "/:username/playlists" => "users#profile"
Something went wrong with that request. Please try again.