Answer the question
In order to leave comments, you need to log in
The form creates and removes a subscription to a user, but js does not replace the subscription form. The form is updated after the page is reloaded. How to fix?
================================================= ===
app/view/users/show.html.haml
= render partial: 'follow_form', locals: {object: @user} if logged_in?
- unless current_user?(object)
#follow_form
- if current_user.following?(object)
= render partial: 'unfollow', locals: {object: object}
- else
= render partial: 'follow', locals: {object: object}
= form_for(current_user.active_relationships.find_by(followed_id: object.id), |
html: { method: :delete}, remote: true) do |f| |
= f.submit "Отписаться", class: "btn btn-success"
$("#follow_form").html("<%= j (render partial: 'users/unfollow', locals: { object: object} ) %>");
$("#followers").html('<%= object.followers.count %>');
$("#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
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2
(1.5ms) BEGIN
SQL (0.8ms) DELETE FROM "relationships" WHERE "relationships"."follower_id" = $1 AND "relationships"."followed_id" = 5
SQL (2.5ms) UPDATE "users" SET "followed_count" = COALESCE("followed_count", 0) - 1 WHERE "users"."id" = 5
(3.3ms) COMMIT
Rendering relationships/destroy.js.erb
Rendered relationships/destroy.js.erb (4856.0ms)
Completed 500 Internal Server Error in 5016ms (ActiveRecord: 11.4ms)
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'
Answer the question
In order to leave comments, you need to log in
I figured it out, in the script it was necessary to declare a controller variable
======================================== ============
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 %>');
$("#follow_form").html("<%= j (render partial: 'users/follow', locals: { object: <b>@user</b>}) %>");
$("#followers").html('<%= <b>@user</b>.followers.count %>');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question