【发布时间】: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