【问题标题】:Ajax comments not working in Rails 3.0.3Ajax 注释在 Rails 3.0.3 中不起作用
【发布时间】:2011-05-30 20:14:57
【问题描述】:

在布局/application.html.erb 中。我有:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>Some title</title>
  <%= stylesheet_link_tag 'application' %>
  <%= stylesheet_link_tag 'blueprint/print', 'media' => 'print' %>
  <%= javascript_include_tag 'jquery', 'jquery-ui-1.8.2.custom', 'jquery.colorbox-min', 'jquery.corner', 'jquery.countdown.min', 'jquery.uploadFileThis',
    'jquery.ui.core', 'jquery.ui.mouse', 'jquery.ui.position', 'jquery.ui.slider', 'jquery.ui.widget', 'jquery.ui.datepicker',
    'ajaxupload', 'jwplayer', 'swfobject', 'flowplayer-3.2.4.min', 'rails', 
    'application', :cache => true %>
  <%= csrf_meta_tag %>
</head>
<body>
.....

在我看来,我有:

<%= link_to 'delete', song_comment_path(comment.media, comment), :confirm => 'Are you sure?', :method => :delete, :remote => true %>

我的 Comments 控制器,destroy 方法如下:

  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to(root_path) }
      format.xml  { head :ok }
      format.js   {  }
    end
  end

当我点击此链接时,我会看到一个弹出框,上面写着“你确定吗?”。我点击确定。我在 /views/cmets 中有一个 destroy.js.erb 文件。在这个文件中,我有:

alert('Inside destroy.js.erb');
// and some code to hide the comment element

评论被删除,但不是通过 ajax。看来,link_to 进入了评论控制器,destroy 方法。但是响应的是respond.html,而不是respond.js。因此,为什么我没有收到 alert('Inside destroy.js.erb');

当我还在使用 Rails 3.0.0 时,这一切都可以正常工作,直到我决定升级:

  • 到 Rails 3.0.3,
  • 从查询 1.4.0 到 1.4.4 (http://jquery.com/),
  • 并更新最新的 rails.js (https://github.com/rails/jquery-ujs)

为什么不调用 format.js?为什么 Rails 还在尝试调用 format.html?

【问题讨论】:

    标签: ruby-on-rails ajax jquery


    【解决方案1】:

    如果您查看由link_to 生成的实际链接,您会发现即使您使用ajax 发送删除请求,您仍然向/song/:song_id/comment/:comment_id 发送请求。因为没有格式,Rails 将默认为.html

    为确保请求格式正确,您可以在 link_to 中添加 :format 选项:

    <%= link_to 'delete', song_comment_path(comment.media, comment, :format => :js), :confirm => 'Are you sure?', :method => :delete, :remote => true %>
    

    现在应该会给你一个这样的链接:/song/:song_id/comment/:comment_id.js

    【讨论】:

    • 此 jquery-ujs 问题中的评论有更多信息:github.com/rails/jquery-ujs/issues/closed#issue/52
    • 提供 :format => :js 是 Rails 3.0.3 的事情还是 github.com/rails/jquery-ujs/issues/closed#issue/52 上的问题的解决方法?此外,github.com/rails/jquery-ujs/issues/closed#issue/52 的开发人员似乎在建议一个补丁。这个补丁已经发布了吗?
    • 我也不明白的是。为什么我们需要指定“.js”扩展名?难道 Rails 不应该足够聪明地找出响应的格式吗? Rails 3.0.0 对此没有任何问题。此外,没有 .js 扩展名的 url 看起来更干净/整洁,不是吗?
    • 我的理解是ajax请求默认不等于js响应格式。请求接受标头指定响应的格式。如果您发出 ajax 请求,您可能希望返回 html,例如插入到页面中。现在,如果 ajax 请求标头当前是“/”,那么将“format.js {}”移动到顶部应该首先提供它,因为 ajax 请求接受所有内容。另一种方法是使用 setRequestHeader 进行 ajax 调用。我自己没有测试过。
    • Heikki,那为什么我必须第一次设置 :format => :js 呢?在 Rails 3.0.0 中,我不必设置它。 Rail 3.0.0 有什么问题吗?我只需要知道设置 :format => :js 是否正确,或者这是否是一种解决方法。
    猜你喜欢
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2014-06-28
    • 2014-08-14
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    相关资源
    最近更新 更多