Form creates and deletes subscriptions to user, but js does not replace subscription form. Form is updated after a page reload. How to fix?

=====================================================
app/view/users/show.html.haml
= render partial: 'follow_form', locals: {object: @user} if logged_in?

=====================================================
app/view/users/_follow_form.html.haml
- unless current_user?(object)
#follow_form
 - if current_user.the following?(object)
 = render partial: 'unfollow', locals: {object: object}
 - else
 = render partial: 'follow', locals: {object: object}

=====================================================
app/view/users/_unfollow.html.haml
= form_for(current_user.active_relationships.find_by(followed_id: object.id), |
 html: { method: :delete}, remote: true) do |f| |
 = f.submit "Unfollow", class: "btn btn-success"

======================================================
app/view/users/_follow.html.haml
= form_for(current_user.active_relationships.build, remote: true) do |f|
%div= hidden_field_tag :followed_id, object.id
= f.submit "Subscribe", class: "btn btn-primary"
======================================================
app/view/relationship/create.js.erb
$("#follow_form").html("<%= j (render partial: 'users/unfollow', locals: { object: object} ) %>");
$("#followers").html('<%= object.followers.count %>');

=======================================================
app/view/relationship/destroy.js.erb
$("#follow_form").html("<%= j (render partial: 'users/follow', locals: { object: object }) %>");
$("#followers").html('<%= object.followers.count %>');

=======================================================
class RelationshipsController < ApplicationController
 before_action :logged_in_user

 def create
 @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)
 respond_to do |format| 
 format.html { redirect_to @user }
format.js
end
end
end

=================================================
and the log in the console

User Load (1.0 ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]]
 (1.5 ms) BEGIN
 SQL (0.8 ms) DELETE FROM "relationships" WHERE "relationships"."follower_id" = $1 AND "relationships"."followed_id" = 5 [["follower_id", 1]]
 SQL (2.5 ms) UPDATE "users" SET "followed_count" = COALESCE("followed_count", 0) - 1 WHERE "users"."id" = 5
 (3.3 ms) COMMIT
 Rendering relationships/destroy.js.erb
 Rendered relationships/destroy.js.erb (4856.0 ms)
Completed 500 Internal Server Error in 5016ms (ActiveRecord: 11.4 ms)



ActionView::Template::Error (undefined local variable or method `object' for #<#<class:0x007fcd6e1a78e8>:0x007fcd6de726a0>
Did you mean? object_id):
 1: $("#follow_form").html("<%= j (render partial: 'users/follow', locals: { object: object }) %>");
 2: $("#followers").html('<%= object.followers.count %>');

app/views/relationships/destroy.js.erb:1:in`_app_views_relationships_destroy_js_erb__1541775164985163323_70260144456500'</class:0x007fcd6e1a78e8>
June 8th 19 at 17:02
1 answer
June 8th 19 at 17:04
Solution
Figured out the script needed to declare a variable controller
======================================================
app/view/relationship/create.js.erb
$("#follow_form").html("<%= j (render partial: 'users/unfollow', locals: { object: <b>@user</b>} ) %>");
$("#followers").html('<%=<b>@user</b>.followers.count %>');

=======================================================
app/view/relationship/destroy.js.erb
$("#follow_form").html("<%= j (render partial: 'users/follow', locals: { object: <b>@user</b>}) %>");
$("#followers").html('<%= <b>@user</b>.followers.count %>');

=======================================================

Find more questions by tags AJAXJavaScriptRuby on Rails