@kasago

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

フォロー機能のカウント部分の非同期通信の実装

解決したいこと

フォロー機能のカウント部分の非同期通信
Ruby on RailsでWebアプリをつくっています。
フォロー機能のカウント部分の非同期通信の実装中にエラーが発生しました。
解決方法を教えて頂きたいです。
宜しくお願い致します。

発生している問題・エラー

フォローボタンを押すと

ActionView::Template::Error (undefined local variable or method `user' for #<#<Class:0x00007f49800ca348>:0x00007f49800f8798>
Did you mean?  @user):

がターミナル上で表示され、カウント部分の表記が変更されません。

該当するソースコード

relationships/create.js.erb

$('#follows_<%= @user.id %>').html("<%= j(render 'relationships/follow', { user: @user }) %>");
$('#follow_<%= @user.id %>').html("<%= j(render 'relationships/follow', { user: @user }) %>");
$("#numeration").html('following : <%= link_to user.followings.count, user_followings_path(user) %>')
$("#numerations").html('followers : <%= link_to user.followers.count, user_followers_path(user) %>')

relationships/destroy.js.erb

$('#follows_<%= @user.id %>').html("<%= j(render 'relationships/follow', { user: @user }) %>");
$('#follow_<%= @user.id %>').html("<%= j(render 'relationships/follow', { user: @user }) %>");
$("#numeration").html('following : <%= link_to user.followings.count, user_followings_path(user) %>')
$("#numerations").html('followers : <%= link_to user.followers.count, user_followers_path(user) %>')


users/_users.html.erb

<div class="search-position">
    <% if user_signed_in? %>
     <%= render 'layouts/searchform' %>
    <% end %>
   </div>
   <h1 class="main-font">Posse</h1>
   <div class="tbl-r04">
    <table class="table">
     <tr class="thead">
       <th class="borderline"></th>
       <th class="main-sub-font">Name</th>
       <th class="borderline"></th>
       <th class="main-sub-font">Relationships</th>
       <th class="borderline"></th>
       <th class="borderline"></th>
     </tr>
     <tr>
      <% users.each do |user| %>
       <td><%= attachment_image_tag user, :profile_image, :fill, 50, 50, format:'jpeg', fallback: "no_image.jpg", size:'50x50' %></td>
       <td><%= user.name %></td>
       <td id="numeration"><p>following : <%= link_to user.followings.count, user_followings_path(user) %></p></td>
       <td id="numerations"><p>followers : <%= link_to user.followers.count, user_followers_path(user) %></p></td>
       <td>
        <div id="follows_<%= user.id %>">
         <%= render 'relationships/follow', user: user %>
        </div>
       </td>
       <td class="link-show"><%= link_to "Show", user_path(user.id) %></td>
     </tr>
     <% end %>
     <%= paginate users %>
   </table>
  </div>
users/index.html.erb

<div class="playlist-body">
 <div class="container">
  <div class="row">
   <div class="col-md-3">

    <h1 class="info-font">Candy info</h1>
     <%= render 'users/profile', user: current_user %>
      <h2 class="link-new"><%= link_to "New playlist", new_playlist_path %></h2>
   </div>

  <div class="col-md-8 offset-md-1">
   <%= render 'users/users', users: @users %>

   </div>
  </div>
 <%= render "layouts/fotter" %>
 </div>
</div>
controller/users_controller.rb

class UsersController < ApplicationController
  before_action :authenticate_user!
  before_action :correct_user, only: [:edit,:update]

  def show
    @user = User.find(params[:id])
    @playlists = @user.playlists
    @playlist = Playlist.new
  end

  def index
    @users = User.page(params[:page]).reverse_order #kaminariの記述
    @playlist = Playlist.new
    @user = current_user
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update(user_params)
      redirect_to user_path(@user.id), notice: "You have updated user successfully."
    else
      render :edit
    end
  end

  private
  def user_params
    params.require(:user).permit(:name, :profile_image, :introduction)
  end

  def correct_user
    @user = User.find(params[:id])
    if @user!=current_user
      redirect_to user_path(current_user)
    end
  end

end

relationships/_follow.html.erb


<div class="follow-btn">
  <% unless current_user == user %>
    <% if current_user.following?(user) %>
      <%= link_to 'Following', user_relationships_path(user.id), method: :delete, remote: true, class: "btn btn-primary test" %>
    <% else %>
      <%= link_to 'Follow', user_relationships_path(user.id), method: :post, remote: true, class: "btn btn-success test" %>
    <% end %>
  <% end %>
</div>

自分で試したこと

フォローボタンの非同期通信の実装はできていると思います。
カウント部分の、jsファイルの記述をコメントアウトするとフォローボタンの切り替わりは実装できていました。

初めての質問で至らない点も多いと思いますが、宜しくお願い致します。

0 likes

No Answers yet.

Your answer might help someone💌