[英]Ajax button no longer updates in Rails app
我实现了Michael Hartl的Rails教程中的“跟随”功能。 它工作好几个月但现在突然一旦退出按钮就不再更新了。 我没有得到任何Javascript错误,也没有在Rails控制台中得到任何错误。 单击该按钮将成功创建/销毁“关系”。 当我尝试再次单击该按钮时,我只在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 .
我//= require jquery_ujs
在application.js中有//= require jquery_ujs
但我不得不将其删除,因为点击Follow按钮会导致点击发生两次。 删除该行解决了这个问题,但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我的网络选项卡:
如果你的create.js.erb
工作(来自你的警报),那么问题就在于你的JS渲染
这是您当前的create.js.erb
:
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= @user.followers.count %>')
#follow_form
是否仍然存在? users/unfollow
吗? #followers
存在吗? 我认为你的create.js.erb
会出现问题。 您可以通过单击right-click
- > inspect element
- > network
- > request you just sent
- 如果有错误,它将是红色
在Js控制台中查找网络标记并检查ajax请求的预览以验证响应服务器的内容,也许ids或类名称不同
发现了这个问题。 我错过了以下表格中的<div id="follow_form">
:
<% unless current_user == @user %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
谢谢大家的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.