Ruby on rails 使用ajax的rails,应对混乱

Ruby on rails 使用ajax的rails,应对混乱,ruby-on-rails,ajax,voting,Ruby On Rails,Ajax,Voting,我有一个投票系统,我希望投票响应是异步的 在我的“秀”观点中,我有 <%= render 'score' %> 我还有一些动作,比如否决投票,但非常相似 我想我真的不确定在format.js块中放什么。我试过一些推杆的方法,比如重定向到lesson路径(lesson)和渲染“score”,但是投票图像没有得到更新 有人能帮忙吗?谢谢 更新@dhoelzgen 我把它改成 def up_vote lesson = Lesson.find(params[:id]) cu

我有一个投票系统,我希望投票响应是异步的

在我的“秀”观点中,我有

<%= render 'score' %>
我还有一些动作,比如否决投票,但非常相似

我想我真的不确定在format.js块中放什么。我试过一些推杆的方法,比如重定向到lesson路径(lesson)和渲染“score”,但是投票图像没有得到更新

有人能帮忙吗?谢谢

更新@dhoelzgen

我把它改成

def up_vote
    lesson = Lesson.find(params[:id])
    current_user.up_vote!(lesson)
    render :score, :layout => false
    #redirect_to lesson_path(lesson)
end
我还将render:score,:layout=>false放在其他操作中

在我看来,我有

<div id="surrounding_container">
    <%= render 'score' %>
</div>
在我的课上,咖啡


我错过什么了吗?图像仍然不会异步更改

如果您有几个选项,我建议您只返回渲染的分数(而不是js/json),然后将其放在(周围的)div标记中

在没有这样的布局的情况下渲染分数

render :score, :layout => false
然后使用JavaScript(在我的示例中是CoffeeScript)替换div标记的内容

$('.up_arrow a')
  .bind 'ajax:success', (event, data) ->
    $('#surrounding_container').html($(data))

或者,您可以将结果返回为json并手动修改页面,但我认为上面的选项会减少工作量。

No,is将只使用render,而不使用stuff.hmmm的repond\u。它似乎不会更改。我遗漏了什么吗?我又稍微编辑了一下我的帖子。我使用
.up\u arrow
类将绑定
ajax:success
的down\u arrow添加到div中。尝试将它绑定到实际的远程链接(而不是周围的div)。对不起,我对这个还是很陌生的。你所说的实际远程链接是什么意思?这个ajax让人困惑…它可以工作=)我所要做的就是用.live-in-the-coffeescript替换.bind
def up_vote
    lesson = Lesson.find(params[:id])
    current_user.up_vote!(lesson)
    render :score, :layout => false
    #redirect_to lesson_path(lesson)
end
<div id="surrounding_container">
    <%= render 'score' %>
</div>
$('.up_arrow')
  .bind 'ajax:success', (event, data) ->
    $('#surrounding_container').html($(data))

$('.down_arrow')
  .bind 'ajax:success', (event, data) ->
    $('#surrounding_container').html($(data))
render :score, :layout => false
$('.up_arrow a')
  .bind 'ajax:success', (event, data) ->
    $('#surrounding_container').html($(data))