weixin_33739523 2014-02-06 11:31 采纳率: 0%
浏览 10

Rails 4 Ajax没有响应

I am struggling to troubleshoot why this ajax call on a standard Destroy method won't work. From looking at my HTML source, I think it might be a problem with either how I'm creating divs using div_for OR it has something to do with my js. I'm at a loss for troubleshooting javascript.

My view:

<% @quizzes.each do |quiz| %>
  <%= div_for quiz do %>
    <tr>
        <td>
            <%= link_to quiz.name, quiz_review_path(quiz.id) %> 
        </td>
        <% if quiz.finished? %>
        <td>
            <%= link_to "Results", quiz_results_path(quiz) %>
        </td>
        <% elsif quiz.questions.first != nil %>
        <td>
            <%= link_to "Take quiz", question_answer_path(question_id: quiz.questions.first.id) %> 
        </td>
        <% else %>
        <td>
            <%= link_to "Broken, delete!", nil %>
        </td>
        <% end %>
        <td>
            <%= link_to "Delete", quiz_path(quiz), method: 'delete', remote: true %>
        </td>
    </tr>
    <% end %>

The controller:

def destroy
    @quiz = Quiz.find(params[:id])
    @quiz.destroy

    respond_to do |format|
        format.html { redirect_to quizzes_path }
        format.js
    end
end

My views/quizzes/destroy.js

$(document).ready(function() {
    $('#<%= dom_id(@quiz) %>').fadeOut(); 
})

From looking at the Rails s logs, I can see that the delete request comes in and is processed by the js, so my best guess is it's either an issue with div_for and dom_id OR my js is bad (highly likely).

  • 写回答

1条回答 默认 最新

  • 旧行李 2014-02-06 12:00
    关注

    Fixed it: trying to wrap in a with div_for creates invalid html. Removed the tables and now the ajax works fine.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?