我在Michael Hartl的Rails教程中实现了“关注”功能。它可以正常工作数月,但是现在按钮一旦退出,突然变得不再更新。我没有任何Javascript错误,也没有在Rails控制台中得到任何错误。单击该按钮将成功创建/销毁“关系”。当我再次尝试单击按钮时,我仅在Rails控制台中收到错误,因为它正在尝试重做事务。
我确实预编译了我的资产,但这从来都不是问题。我一直都会对我的资产进行预编译,然后将应用程序推到Heroku上,然后按钮就可以正常工作了。我最近实现的唯一更改是添加了simple_form和haml-rails gem,以使邮箱gem正常工作。我不确定它们是否相关,但我认为这可能有助于调试。
这个中断是如何发生的,我该如何解决?谢谢!
相关代码:
控制器:
class RelationshipsController < ApplicationController
def create
@user = User.find(params[:relationship][: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
按照表格
<% unless current_user == @user %>
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
跟随
<%= form_for(current_user.relationships.build(followed_id: @user.id),
remote: true) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-primary btn-small" %>
<% end %>
取消关注
<%= form_for(current_user.relationships.find_by_followed_id(@user),
html: { method: :delete },
remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-primary btn-small" %>
<% end %>
create.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= @user.followers.count %>')
destroy.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>")
$("#followers").html('<%= @user.followers.count %>')
application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//= require bootstrap
//= require jquery
//= require jquery-ui
//= require jquery.tokeninput
//= require_tree .
我曾经//= require jquery_ujs
在application.js中使用过,但是我必须将其删除,因为单击“跟随”按钮将导致单击发生两次。删除该行解决了该问题,但是Ajax似乎不再更新按钮了,我不知道为什么。
编辑:
Rails控制台确实显示正在接收请求。我什至可以刷新页面,然后按钮将更新:
Started DELETE "/relationships/32" for 127.0.0.1 at 2014-01-13 21:35:37 -0500
Processing by RelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"---edited---", "commit"=>"Unfollow", "id"=>"32"}
Relationship Load (0.4ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."id" = $1 LIMIT 1 [["id", "32"]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Relationship Load (0.4ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = 1 AND "relationships"."followed_id" = 2 LIMIT 1
(0.1ms) BEGIN
SQL (0.3ms) DELETE FROM "relationships" WHERE "relationships"."id" = $1 [["id", 32]]
(1.5ms) COMMIT
Rendered users/_follow.html.erb (2.5ms)
(0.7ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = 2
Rendered relationships/destroy.js.erb (6.0ms)
Completed 200 OK in 17.4ms (Views: 8.0ms | ActiveRecord: 4.2ms)
Javascript控制台在Chrome中根本不显示任何错误。
编辑2我的网络标签:
找到了问题。我<div id="follow_form">
在以下表单上丢失了:
<% unless current_user == @user %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
谢谢大家的帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句