A
A
alx2018-02-01 16:09:09
JavaScript
alx, 2018-02-01 16:09:09

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?

================================================= ===
app/view/users/_follow_form.html.haml
- unless current_user?(object)
  #follow_form
    - if current_user.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 "Отписаться", 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 log in console

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

1 answer(s)
A
alx, 2018-02-01
@Lifehack043

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 %>');

================================================= =====
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 %>');

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question