Rails 3 Ajax响应未执行 [英] Rails 3 ajax response is not executed

查看:99
本文介绍了Rails 3 Ajax响应未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中创建了一个简单的ajax功能,用户可以通过链接增加点赞次数".

I am creating a simple ajax feature into my application where a user can increase a "like count" through a link.

我设法获得了运行控制器代码的链接,并且浏览器正在获得响应,但响应未执行.

I have managed to get the link to run my controller code and the browser is getting a response back, but the response is not executed.

like链接的一部分:

The like link is in partial:

.likes                      
  .like_up                                                                  
    = link_to( image_tag('/images/thumb_up.png'), '/like/' + question.id.to_s, :remote => true)
  #like_count
    = 22# question.likecount                                                
  .like_down
    = link_to( image_tag('/images/thumb_down.png'), '/dislike/' + question.id.to_s, :remote => true)

它进入了likes_controller:

And it goes into likes_controller:

def up
  @like = current_user.likes.create( :question_id => params[:question_id], :like => true )
  respond_to do |format|
    format.js
  end
end

然后在我的up.js.haml中,我只拥有一个调试javascript:

Then in my up.js.haml I simply have a debug javascript:

alert('2');

没别的.

当我在浏览器中按下like链接时,rails日志告诉我它已经呈现了js.haml

When I press the like link in browser the rails log tels me that it has rendered the js.haml

...
Rendered likes/up.js.haml within layouts/application (104.9ms)
Completed 200 OK in 923ms (Views: 116.3ms | ActiveRecord: 20.1ms)

当我查看响应时,浏览器得到的响应肯定看起来像up.js.haml渲染到放置了yield标签的应用程序布局中.

And when I look at the response the browser gets it certainly looks like the up.js.haml rendered into my application layout where the yield tag is placed.

  <div class='content'>
    alert('2');
  </div>

我怀疑这不是浏览器想要恢复的.同样,在ajax请求期间查看firebug控制台时,发送请求后也没有任何反应.

I suspect that this is not what the browser wants to get back. Also when looking at the firebug console during the ajax request, nothing happens after the request is sent.

有人能找出为什么这行不通吗?我发现的每个教程和指南都以与我在此处相同的方式执行此操作.

Could someone find out why this is not working? Every tutorial and guide I have found does this the same way I'm doing here.

推荐答案

首先,确保内容类型正确:

First, make sure the content-type is correct:

curl --head -X POST http://localhost:8080/....

我相信Content-Type应该是text/javascript

第二,我想您要禁用布局,以使DIV不会出现在输出中.

Second, I think you want to disable the layout so that the DIV doesn't appear in the output.

def up
  @like = current_user.likes.create( :question_id => params[:question_id], :like => true )
  respond_to do |format|
    format.js { render :partial => 'up', :layout => false }
  end
end

或者可以在整个控制器中将其禁用

Or maybe disable it in the whole controller with

layout false

这篇关于Rails 3 Ajax响应未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆