2011-12-24 29 views
0

我有一个网站(GiggleTrigger.com),用户可以投票表达他们认为有趣的笑话(通过点击“咯咯”按钮) 。这是通过ajax调用发生的,但现在升级到Rails 3.1后,我似乎破坏了这一点。我试图更新JavaScript到jQuery,但它仍然无法正常工作。升级到Rails 3.1,更改为jQuery,现在我的ajax似乎没有更新字段

这里是旧的JavaScript代码(放置在视图/票/ create.js.rjs):

page.replace_html "vote_total_#{@punchline.id}", "#{@punchline.votes.size}" 

这里是所尝试的更新的代码(放置在视图/票/ create.js .erb):

$("#vote_total_#{@punchline.id}").html("<%= escape_javascript render @punchline.votes.size %>"); 

下面是HTML(放置在视图/ punchlines/_punchline.html.erb):

<span id="vote_total_<%= punchline.id %>" class="punchline_votes"> 
    <%= punchline.votes.size %> 
</span> 
<span id="vote_button"> 
    <%= button_to 'giggle', punchline_votes_path(:punchline_id => punchline), :remote => true %> 
</span> 

这里是控制器代码(放置在控制器/ votesController.rb):

class VotesController < ApplicationController 
    def create 
    @punchline = Punchline.find(params[:punchline_id]) 
    @vote  = @punchline.votes.build params[:vote] 
    @vote.user = current_user 

    respond_to do |format| 
     if @vote.save 
     format.js   
     format.html { redirect_to @punchline } 
     else 
     format.html { redirect_to root } 
     end 
    end 
    end 
end 

*我正在此错误:

ActionView::Template::Error (undefined method `model_name' for Fixnum:Class): 
    1: $("#vote_total_#{@punchline.id}").html("<%= escape_javascript render @punchline.votes.size %>"); 
     app/views/votes/create.js.erb:1:in `_app_views_votes_create_js_erb__1782616409977082835_2168430380' 
     app/controllers/votes_controller.rb:7:in `create 

由于这似乎是一个整数传递的问题,我添加.to_s方法,如下所示:

$("#vote_total_#{@punchline.id}").html("<%= escape_javascript render @punchline.votes.size.to_s %>"); 

但我得到这个错误:

ActionView::Template::Error (Missing partial votes/1, application/1 with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in: 
    * "/Users/Daniel/Sites/giggle/app/views" 
    * "/Users/Daniel/.rvm/gems/[email protected]/gems/devise-1.5.3/app/views" 
): 
    1: $("#vote_total_#{@punchline.id}").html("<%= escape_javascript render @punchline.votes.size.to_s %>"); 
    app/views/votes/create.js.erb:1:in `_app_views_votes_create_js_erb__1782616409977082835_2187483480' 
    app/controllers/votes_controller.rb:7:in `create' 

我尝试添加的部分,像这样:

$("#vote_total_#{@punchline.id}").html("<%= escape_javascript render :partial => 'punchline', :object => @punchline.votes.size %>"); 

但后来我得到这个错误:

ActionView::Template::Error (Missing partial votes/punchline, application/punchline with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in: 
    * "/Users/Daniel/Sites/giggle/app/views" 
    * "/Users/Daniel/.rvm/gems/[email protected]/gems/devise-1.5.3/app/views" 
): 
    1: $("#vote_total_#{@punchline.id}").html("<%= escape_javascript render :partial => 'punchline', :object => @punchline.votes.size.to_s %>"); 
    app/views/votes/create.js.erb:1:in `_app_views_votes_create_js_erb__1782616409977082835_2185760740' 
    app/controllers/votes_controller.rb:7:in `create' 

当我单击giggle按钮时,投票被添加到模型对象,但它不会更新视图,除非我刷新页面。很显然,我希望通过ajax而不需要刷新。谢谢!

+0

值来确定。这个?然后插入数组中的值(请参阅日志行的其余部分) – BlueFish 2011-12-24 07:32:36

+0

尝试“puts params.inspect”并查看您是否正在获取post请求中的值... – BlueFish 2011-12-24 07:33:40

+0

定义“not working” - 外观就像发送ID并且动作正在发射一样。 – 2011-12-24 07:35:14

回答

0

好的,我明白了。旧的原型代码:

page.replace_html "vote_total_#{@punchline.id}", "#{@punchline.votes.size}" 

使用#{}将ruby数据发布到元素中。但jQuery使用#来表示一个id。于是,我改变了我的jQuery选择从#{}来<%=%>,它解决了这个问题:(?,?,?,)

$("#vote_total_<%= @punchline.id %>").html("<%[email protected]%>"); 
1

您可以添加

if(!document.getElementById("vote_total_#{@punchline.id}")) { 
    alert("Id doesn't exist: " + "vote_total_#{@punchline.id}"); 
} 

给JavaScript,并且会提醒你,如果ID是坏的。不正确的意思是一个具有该ID的元素不存在。我很好奇为客户端产生了什么,这是ajax,大部分都是客户端的东西。

您也应该检查控制台(萤火虫或WebKit的检查),并打开XMLHttpRequest的日志记录,并查看是否有真正的AJAX正在做的,当你点击咯咯:d

- 对于WebKit的检查,有窗口右下角的齿轮图标。点击这个,你会看到记录XHR的选项。

- 如果有人可以对Firebug发表评论,那就太棒了。

+0

该ID很好。 XMLHttpRequest日志记录已打开,但未给我任何错误。 – thatdankent 2011-12-24 21:41:26

+0

是否有任何XHR发生? – 2011-12-25 02:28:21

+0

我不知道该如何回答。我认为有。 – thatdankent 2011-12-25 03:56:34