我没有在users / index.HTML.erb中使用unfollow / follow partials,因为index.HTML.erb中没有@user变量,它对@users进行了分页,所以我使用了user.ID.
class RelationshipsController < ApplicationControllerbefore_action :logged_in_userdef 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 endend**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 > <% @users.in_groups_of(3,false).each do |users| %> <div > <% users.each do |user| %> <ul > <% 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 > <div > <a href="<%= following_user_path(user.ID) %>"> <center><strong ID="following" class:"stat"> <%= user.following.count %><br> following </strong></center> </a> </div> <div > <a href="<%= followers_user_path(user.ID) %>"> <center><strong ID="followers" > <%= user.followers.count %><br> followers </strong></center> </a> </div> <div > <center><strong ID="photo-count" > <%= user.photos.count %><br> photos shared </strong></center> </div> </div> </div> <% end %> </div> <% end %></div><%= will_paginate %>**users/show.HTML.erb** ... <li > <%= render 'follow_form' if logged_in? %> </li> </ul> </div> <div > <div > <a href="<%= following_user_path(@user) %>"> <center><strong ID="following" class:"stat"> <%= @user.following.count %><br> following </strong></center> </a> </div> <div > <a href="<%= followers_user_path(@user) %>"> <center><strong ID="followers" > <%= @user.followers.count %><br> followers </strong></center> </a> </div> <div > <center><strong ID="photo-count" > <%= @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),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.erb和create.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 +0000Processing by RelationshipsController#destroy as JsRendered 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" = [0m [["followed_ID",1]]Started POST "/relationships" for 184.90.97.154 at 2016-05-13 20:07:26 +0000Processing by RelationshipsController#create as Js[1m[36m (1.5ms)[0m [1mCOMMIT[0m解决方法 我复制了你的代码,在添加下面的修改后,它完美地运行了. (即我不需要为“关注/取消关注”按钮更改页面重新加载)
请尝试以下 *** 作以查看其是否有效:
users / index.HTML.erb(这里只有相关代码,即“Follow /”取消关注“按钮)
<ul > <% 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),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}%>.可选的散列本地:{…….}允许您将局部变量传递给想要渲染的局部变量(_partial.HTML.erb)在这种情况下,您还需要在名称前添加partial:您要渲染的部分.
这样,您的用户本地变量不仅可以在users / index.HTML.erb中使用,还可以在上面列出的其他文件中使用.
总结以上是内存溢出为你收集整理的ruby-on-rails – 使用follow / unfollow按钮对用户进行分页:由于重复的类名,Ajax会遇到问题全部内容,希望文章能够帮你解决ruby-on-rails – 使用follow / unfollow按钮对用户进行分页:由于重复的类名,Ajax会遇到问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)