我实现了Michael Hartl的Rails教程中的“跟随”功能。它在几个月内运行良好,但现在突然间,按钮不再更新,一旦退出。我没有得到任何Javascript错误,也没有在Rails控制台中得到任何错误。点击按钮将成功创建/销毁“关系”。当我尝试再次单击按钮时,我只在Rails控制台中出现错误,因为它试图重做事务。Ajax按钮不再在Rails应用程序中更新
我做了预编译我的资产,但这从来都不是问题。我一直预编译我的资产,并将应用程序推送到Heroku,并且按钮正常工作。我最近实施的唯一更改是添加了simple_form和haml-rails gems以使邮箱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 .
我曾经有过的的application.js //= require jquery_ujs
但我不得不删除它,因为点击Follow按钮会导致点击发生两次。删除该行解决了这个问题,但Ajax似乎并没有更新按钮,我不知道为什么。
编辑:
滑轨控制台它只表示该请求正在被接收。我甚至可以刷新页面,然后按钮将更新:
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中根本不显示任何错误。
EDIT 2 我的网络标签:
浏览器和服务器控制台在诊断AJAX交互时很有用:1.浏览器发送请求吗? 2.服务器正在接收它吗? 3.是Rails过滤参数吗? – juanpastas
感谢您的回复!编辑的问题。 Rails控制台显示请求正在完成。它甚至说“呈现关系/ create.js.erb”。 Chrome中的Javascript控制台中没有发现错误 – winston
在create.js.erb中添加alert(“test”)''是否会弹出? – jason328