【问题标题】:rails with ajax, respond_to confusion带有ajax的rails,respond_to混乱
【发布时间】:2012-04-17 07:04:01
【问题描述】:

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

在我的“展示”视图中,我有...

<%= render 'score' %>

渲染

<% if @user.voted?(@lesson) %>
    <%= render 'voted' %>
<% else %>
    <%= render 'unvoted' %>
<% end %>

然后渲染我的 _voted.html.erb

    #will display different images, colored or not based on selection
    <% if @user.up_voted?(@lesson) %>
        <div class = "up_arrow">
            <%= link_to image_tag("up_color.png", alt: "Uncheck vote"), 
                clear_vote_lesson_url(@lesson), :method => :post, remote: true %>
        </div>
        <div class="average_score"><%= @lesson.votes %></div>
        <div class= "down_arrow">
            <%= link_to image_tag("down_uncolor.png", alt: "Vote down"), 
                down_vote_lesson_url(@lesson), :method => :post, remote: true %>
        </div>
    <!-- user down voted then -->
    <% else %>
        <div class = "up_arrow">
            <%= link_to image_tag("up_uncolor.png", alt: "Vote up"), 
                up_vote_lesson_url(@lesson), :method => :post, remote: true %>
        </div>
        <div class="average_score"><%= @lesson.votes %></div>
        <div class= "down_arrow">
            <%= link_to image_tag("down_color.png", alt: "Uncheck vote"), 
                clear_vote_lesson_url(@lesson), :method => :post, remote: true %>
        </div>
    <% end %>

我的 unvoted.html.erb 是

<div class = "up_arrow">
<%= link_to image_tag("up_uncolor.png", alt: "Vote up"), 
    up_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>
<div class="average_score"><%= @lesson.votes %></div>
<div class= "down_arrow">
    <%= link_to image_tag("down_uncolor.png", alt: "Vote down"), 
        down_vote_lesson_url(@lesson), :method => :post, remote: true %>
</div>

最后在我的控制器中我有..

    def up_vote
    lesson = Lesson.find(params[:id])
    current_user.up_vote!(lesson)

    respond_to do |format|
        format.html { render :action => 'show'}
        format.js {   ????????        }
    end
end

我还有一些动作,例如 down_vote,但它们非常相似。

我想我不太确定在我的 format.js 块中放什么。我尝试将诸如 redirect_to course_path(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

我也将渲染 :score, :layout => false 放在我的其他操作中。

在我看来我有

<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))

在我的课程.js.coffee 中。

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

【问题讨论】:

    标签: ruby-on-rails ajax voting


    【解决方案1】:

    你有几个选择,我建议只返回渲染的分数(不是 js / json),然后把它放到(周围的)div 标签中。

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

    render :score, :layout => false
    

    然后使用JavaScript(在我的例子中是CoffeeScript)来替换div标签的内容

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

    或者,您可以将结果返回为 json 并手动修改页面,但我认为上面的选项会更省力。

    【讨论】:

    • 不,只是使用渲染,没有 repond_to 的东西。
    • 嗯,它似乎没有改变。我错过了什么吗?我再次稍微编辑了我的帖子。我添加了向下箭头
    • 您将ajax:success 绑定到您的div 与.up_arrow 类。尝试将其绑定到实际的远程链接(而不是周围的 div)。
    • 对不起,我对此还是很陌生。实际的远程链接是什么意思?这个 ajax 令人困惑......
    • 它有效 = ) 我所要做的就是用咖啡脚本中的 .live 替换 .bind
    猜你喜欢
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多