rails jquery ajax request not executing
rails jquery ajax request not executing
我正在构建的应用程序上实现一个投票系统,与堆栈溢出上的应用程序非常相似。当用户单击赞成或反对时,ajax 请求将发送到我的一个控制器,他们更新几个表。完成后,我使用 respond_to 路由到 js.erb 文件,该文件执行 jquery 以更新用户显示。但是,jquery 未被执行。当我舔赞成/反对时,控制器代码执行正常(我可以在 rails 控制台中看到它),但用户显示没有更新。但是,刷新时,用户显示不会异步更新。我知道 jquery 没有在火虫控制台中执行 b/c 我可以看到我的 jquery 代码 - 它只是没有被应用。这是我的赞成票控制器代码:
def upvote
@post = Post.find(params[:id])
@user_vote = current_user.votes.where(post_id: @post.id).first
if @user_vote.blank?
Vote.create(user_id: current_user.id, post_id: @post.id, vote: 1)
@post.upvotes += 1
elsif @user_vote.vote.to_i == 0
@user_vote.vote = 1
@post.upvotes += 1
elsif @user_vote.vote.to_i == 1
return redirect_to :back, :alert => 'You have already upvoted the post'
elsif @user_vote.vote.to_i == 2
@user_vote.vote = 1
@post.downvotes -= 1
@post.upvotes += 1
end
respond_to do |format|
if @post.save and @user_vote.save
format.js { }
else
format.html { redirect_to :back, :alert => 'There was an error in upvoting the post' }
end
end
end
这是我的赞成.js.erb文件:
$('#post-action-<%= @post.id %>').html("upvote");
// this normally renders a partial (below), but I am rendering "upvote" until I can fix this
// escape_javascript(<%= render :partial => 'post_voting', :locals => { post: @post, user_vote: @user_vote } %>)
这是我的网页:
<div class="post_actions" id='post-action-<%= "#{post.id}" %>' >
<%= render :partial => 'post_voting', :locals => { post: post, user_vote: current_user.votes.where( post_id: post.id ).first } %>
</div>
这是我的火虫控制台输出:https://i.stack.imgur.com/ntXeM.png
编辑
我注意到我的服务器上出现错误:
Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2012-08-09 06:33:42 -0700
Served asset /bootstrap.js - 304 Not Modified (0ms)
您的代码似乎没问题,应该执行。您可以执行其他检查以更新固定 ID 而不是计算的 ID。
$('#updateme').html("upvote");
并将此 ID 添加到您的 HTML 中的某个位置。如果这有效,您确定 ajax-call 完全执行,问题只是您引用的 id。
您是否检查过您的 html 源代码是否有 ID "post-action-2"?
在弄乱了jquery之后,我注意到了一些事情。首先,我需要缩小我的代码(删除所有注释和未使用的空格)。我拿了这个:
$('#post-action-<%= @post.id %>').html("upvote");
// this normally renders a partial (below), but I am rendering "upvote" until I can fix this
// escape_javascript(<%= render :partial => 'post_voting', :locals => { post: @post, user_vote: @user_vote } %>)
并将其更改为:
$('#post-action-<%= @post.id %>').html("upvote");
然后代码开始工作,用户显示正在更新以显示"赞成"。完善。所以然后我用这个替换了上面的代码:
$('#post-action-<%= "#{@post.id}" %>').html("<%= escape_javascript(render :partial => 'post_voting', :locals => { :post => @post, :user_vote => @user_vote }) %>");
和卡波!它有效。很高兴解决了这个问题!缩小你的JQUERY人员!
相关文章:
- jQuery is not loaded
- AngularJS JSON not arriving php
- $rootScope not working
- reactRedux is not defined
- jquery ajax success functions not executing
- rails jquery ajax request not executing
- Javascript not executing google chrome extensioin
- SoundCloud JS SDK get handler not executing
- Office 365 on iPad - SP.SOD.executeFunc not executing functi
- Webview javascript not executing
- Rails 3.2.2 not executing rjs
- setTimeout not executing
- jQuery .post() not executing
- DataTables Ajax not executing
- Cordova / Phonegap -- jquery not executing Javascript on <
- Angularjs $timeout not executing
- Delphi, EmbeddedWB/TWebbrowser - jQuery not executing
- JS not executing
- Jquery else not executing
- e.preventDefault not executing