ruby-on-rails - 使用关注/取消关注按钮对用户进行分页 : Ajax runs into problems because of duplicate class names

标签 ruby-on-rails ajax pagination

我的 users/show.html.erb 页面工作正常,因为每个页面上只显示一个类名。当用户在 users/index.html.erb 中分页时连同 follow/unfollow buttons relationship/js.create.erbrelationship/js.destroy.erb变得古怪,因为页面上有重复的类名,(第一个分页用户更新,无论哪个用户被关注/取消关注,这只会在页面刷新时自行修复(实际模型仍然正常工作))。

我没有在 users/index.html.erb 中使用 unfollow/follow 部分因为没有 @user index.html.erb 中的变量,它对@users 进行分页,所以我使用 user.id .

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

**relationships/create.js.erb**
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>");
$("#followers").html('<%= @user.followers.count %> <br> followers');

**relationships/destroy.js.erb**
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>");
$("#followers").html('<%= @user.followers.count %> <br> followers');

**users/index.html.erb** 
<div class="row community">
  <% @users.in_groups_of(3, false).each do |users| %>
    <div class="col-sm-12">
      <% users.each do |user| %>
            <ul class="name-head">
              <% unless current_user == user %>
                <div id="follow_form">
                  <% if current_user.following?(user) %>
                    <%= form_for(current_user.active_relationships.find_by(followed_id: user.id),
                    html: { method: :delete }, remote: true) do |f| %>
                        <%= f.submit "Unfollow", class: "btn btn-primary" %>
                    <% end %>
                  <% else %>
                    <%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
                      <div><%= hidden_field_tag :followed_id, user.id %></div>
                      <%= f.submit "Follow", class: "btn btn-primary" %>
                    <% end %>
                  <% end %>
                </div>
              <% end %>
            </ul>
          <div class="col-xs-12">
          <div class="col-xs-4">
            <a href="<%= following_user_path(user.id) %>">
              <center><strong id="following" class:"stat">
              <%= user.following.count %><br>
              following
              </strong></center>
            </a>  
          </div>
          <div class="col-xs-4">
            <a href="<%= followers_user_path(user.id) %>">
              <center><strong id="followers" class="stat">
                <%= user.followers.count %><br>
                followers
              </strong></center>
            </a>
          </div>
          <div class="col-xs-4">
              <center><strong id="photo-count" class="stat">
                <%= user.photos.count %><br>
              photos shared
              </strong></center>
          </div>
        </div>
        </div>  
      <% end %>
    </div>  
  <% end %>
</div>
<%= will_paginate %>

**users/show.html.erb** 
.
.
.
      <li class="follow">
        <%= render 'follow_form' if logged_in? %>
      </li>
    </ul>
  </div>
  <div class="col-xs-12">
    <div class="col-xs-4">
      <a href="<%= following_user_path(@user) %>">
        <center><strong id="following" class:"stat">
        <%= @user.following.count %><br>
        following
        </strong></center>
      </a>  
    </div>
    <div class="col-xs-4">
      <a href="<%= followers_user_path(@user) %>">
        <center><strong id="followers" class="stat">
          <%= @user.followers.count %><br>
        followers
        </strong></center>
      </a>
    </div>
    <div class="col-xs-4">
        <center><strong id="photo-count" class="stat">
          <%= @user.photos.count %><br>
        photos shared
        </strong></center>
    </div>
end

**_follow_form.html.erb**
<% unless current_user?(@user) %>
  <div id="follow_form">
  <% if current_user.following?(@user) %>
    <%= render 'unfollow' %>
  <% else %>
    <%= render 'follow' %>
  <% end %>
  </div>
<% end %>

**_follow.html.erb**
<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
  <div><%= hidden_field_tag :followed_id, @user.id %></div>
  <%= f.submit "Follow", class: "btn btn-primary" %>
<% end %>

**_unfollow.html.erb**
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id),
         html: { method: :delete },
         remote: true) do |f| %>
   <%= f.submit "Unfollow", class: "btn btn-primary" %>
 <% end %>

编辑:我想过将 user_id 传递到类中,例如:
<div id="follow_form<%="#{user.id}"%>"> 

which created <div id="follow3">

但是我不知道如何将其传递给 destroy.js.erbcreate.js.erb
I have tried many combinations such as:

 $("#follow_form<%="#{user.id}"%>").html("<%= escape_javascript(render('users/unfollow')) %>");


 $("#follow_form<%= @user.id%>").html("<%= escape_javascript(render('users/unfollow')) %>"); 

然而,代码仍然不起作用,即使它起作用了,它仍然需要工作,因为更改 create/destroy.js.erb 文件会破坏 users/show.html.erb 关注/取消关注按钮。

编辑:我也发现了这个 question这是相似的,但它不包含可接受的答案。

编辑 Heroku 日志:这是我在 users/index.html.erb 页面上关注/取消关注用户时得到的结果:
Started DELETE "/relationships/360" for 184.90.97.154 at 2016-05-13 20:05:17 +0000
Processing by RelationshipsController#destroy as JS
Rendered users/_follow.html.erb (1.6ms)
 (1.1ms)[0m  [1mSELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = $1[0m  [["followed_id", 1]]

Started POST "/relationships" for 184.90.97.154 at 2016-05-13 20:07:26 +0000
Processing by RelationshipsController#create as JS
[1m[36m (1.5ms)[0m  [1mCOMMIT[0m

最佳答案

我已经复制了您的代码,在添加以下修改后,它运行良好。 (即,我不需要重新加载页面以更改“关注/取消关注”按钮)

尝试以下操作,看看它是否有效:

users/index.html.erb(这里只有相关代码,即“关注/”取消关注“按钮)

<ul class="name-head">
  <% unless current_user == user %>
     <%= render partial: 'follow_form', locals: { user: user } %>
  <% end %>
</ul>

用户/_follow_form.html.erb
<div id="follow_form-<%= user.id %>">
  <% if current_user.following?(user) %>
    <%= render partial: 'unfollow', locals: { user: user } %>
  <% else %>
   <%= render partial: 'follow', locals: { user: user } %>
  <% end %>
</div>

用户/_unfollow.html.erb
<%= form_for(current_user.active_relationships.find_by(followed_id: user.id), html: { method: :delete }, remote: true) do |f| %>
    <%= f.submit "Unfollow", class: "btn-default" %>
<% end %>

用户/_follow.html.erb
<%= form_for(current_user.active_relationships.build, remote: true) do |f| %>
    <div><%= hidden_field_tag :followed_id, user.id %></div>
    <%= f.submit "Follow", class: "btn" %>  
<% end %>

关系/create.js.erb
$("#follow_form-<%= @user.id %>").html("<%= escape_javascript(render(partial: 'users/unfollow', locals: { user: @user })) %>");

关系/destroy.js.erb
$("#follow_form-<%= @user.id %>").html("<%= escape_javascript(render(partial: 'users/follow', locals: { user: @user })) %>");

注意使用 <%= render partial: "_partial.html.erb", locals: { var_name: var } %> .可选的哈希 locals: { ....... }允许您将局部变量传递给要渲染的局部变量 ( _partial.html.erb ) 在这种情况下,您还需要添加 partial:在要渲染的部分名称之前。

这样,您的 user局部变量将不仅在 users/index.html.erb 中可用,但也在上面列出的其他文件中。

关于ruby-on-rails - 使用关注/取消关注按钮对用户进行分页 : Ajax runs into problems because of duplicate class names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37124193/

相关文章:

ruby-on-rails - 稍后交付在 Rails 5 的测试环境中不起作用

ruby-on-rails - Create_Association 不起作用

jquery - Sinatra View 在某些删除请求后不会刷新

Laravel 分页在第一页后不起作用

php - 已弃用的 Ajax 或 JQuery 命令导致无结果返回

ruby-on-rails - Redis EXECABORT 事务因先前的错误而被丢弃。 (Redis::CommandError) Sidekiq 错误

ruby-on-rails - 设计模式和设计原则有什么区别?

成功函数后javascript返回数据

php - 从 AJAX 获取返回值

pagination - Vue.js - 分页