@Lifehack043

Форма создает и удаляет подписку на юзера, но js не заменяет форму подписки. Форма обновляется после перезагрузки страницы. Как исправить?

=====================================================
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 "Подписаться", 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

=================================================
и лог в консоли

User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
   (1.5ms)  BEGIN
  SQL (0.8ms)  DELETE FROM "relationships" WHERE "relationships"."follower_id" = $1 AND "relationships"."followed_id" = 5  [["follower_id", 1]]
  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'
  • Вопрос задан
  • 29 просмотров
Решения вопроса 1
@Lifehack043
Разобрался, в скрипте нужно было объявить переменную контроллера
======================================================
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 %>');

=======================================================
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через TM ID
Похожие вопросы