diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb
index 6fc33a9..e9dbff9 100644
--- a/app/controllers/relationships_controller.rb
+++ b/app/controllers/relationships_controller.rb
@@ -2,14 +2,20 @@ class RelationshipsController < ApplicationController
before_action :logged_in_user
def create
- user = User.find(params[:followed_id])
- current_user.follow(user)
- redirect_to user
+ @user = User.find(params[:followed_id])
+ current_user.follow(@user)
+ respond_to do |format|
+ format.html { redirect_to @user }
+ format.js
+ end
end
def destroy
- user = Relationship.find(params[:id]).followed
- current_user.unfollow(user)
- redirect_to user
+ @user = Relationship.find(params[:id]).followed
+ current_user.unfollow(@user)
+ respond_to do |format|
+ format.html { redirect_to @user }
+ format.js
+ end
end
end
diff --git a/app/views/relationships/create.js.erb b/app/views/relationships/create.js.erb
new file mode 100644
index 0000000..6adae73
--- /dev/null
+++ b/app/views/relationships/create.js.erb
@@ -0,0 +1,2 @@
+$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
+$("#followers").html('<%= @user.followers.count %>')
\ No newline at end of file
diff --git a/app/views/relationships/destroy.js.erb b/app/views/relationships/destroy.js.erb
new file mode 100644
index 0000000..6382b4a
--- /dev/null
+++ b/app/views/relationships/destroy.js.erb
@@ -0,0 +1,2 @@
+$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>")
+$("#followers").html('<%= @user.followers.count %>')
\ No newline at end of file
diff --git a/app/views/users/_follow.html.erb b/app/views/users/_follow.html.erb
index 6fba8f5..ac21b5d 100644
--- a/app/views/users/_follow.html.erb
+++ b/app/views/users/_follow.html.erb
@@ -1,4 +1,4 @@
-<%= form_for(current_user.active_relationships.build) do |f| %>
+<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
<%= hidden_field_tag :followed_id, @user.id %>
<%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>
\ No newline at end of file
diff --git a/app/views/users/_unfollow.html.erb b/app/views/users/_unfollow.html.erb
index f73fa62..bfab1ad 100644
--- a/app/views/users/_unfollow.html.erb
+++ b/app/views/users/_unfollow.html.erb
@@ -1,4 +1,5 @@
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),
- html: { method: :delete }) do |f| %>
+ html: { method: :delete },
+ remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
\ No newline at end of file
diff --git a/config/application.rb b/config/application.rb
index 43ba68a..d58bb55 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -22,5 +22,6 @@ class Application < Rails::Application
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
+ config.action_view.embed_authenticity_token_in_remote_forms = true
end
end
diff --git a/test/integration/following_test.rb b/test/integration/following_test.rb
index 02f0ec2..ad93590 100644
--- a/test/integration/following_test.rb
+++ b/test/integration/following_test.rb
@@ -3,7 +3,8 @@
class FollowingTest < ActionDispatch::IntegrationTest
def setup
- @user = users(:michael)
+ @user = users(:michael)
+ @other = users(:archer)
log_in_as(@user)
end
@@ -24,4 +25,32 @@ def setup
assert_select "a[href=?]", user_path(user)
end
end
+
+ test "should follow a user the standard way" do
+ assert_difference '@user.following.count', 1 do
+ post relationships_path, followed_id: @other.id
+ end
+ end
+
+ test "should follow a user with Ajax" do
+ assert_difference '@user.following.count', 1 do
+ xhr :post, relationships_path, followed_id: @other.id
+ end
+ end
+
+ test "should unfollow a user the standard way" do
+ @user.follow(@other)
+ relationship = @user.active_relationships.find_by(followed_id: @other.id)
+ assert_difference '@user.following.count', -1 do
+ delete relationship_path(relationship)
+ end
+ end
+
+ test "should unfollow a user with Ajax" do
+ @user.follow(@other)
+ relationship = @user.active_relationships.find_by(followed_id: @other.id)
+ assert_difference '@user.following.count', -1 do
+ xhr :delete, relationship_path(relationship)
+ end
+ end
end