如果你愿意学习一些JavaScript,我发现做同样的东西,在jQuery的是快速和容易。这听起来像你还在做AJAX调用,只是link_to_remote
行为有不幸的副作用。
如果你的Rails视图代码如下所示:
<%= link_to "Show me", slow_page_url, :class => 'remote', :'data-update' => 'display' %>
我想补充一个jQuery块,像这样:
<% content_for :dom_ready do %>
// register a callback for the click event on links with class 'remote'
$('a.remote').click(function() {
// this is the clicked_link, which you may want to
// persist thru several levels of callbacks
// so assign it to a local variable
var clicked_link = this;
// assuming you already have a start_spinner function,
// and you might pass it the object where the click occured
start_spinner(clicked_link);
// here's the ajax call, which will automatically update
// the div you identified using the data-update attribute
// with the returned html
$('#'+$(this).attr('data-update')).load($(this).attr('href'), {}, function() {
//this callback only runs when the AJAX request has completed
stop_spinner(clicked_link);
});
// prevents the click event from propagating to the window object
//and moving the user away from the current page
return false;
});
<% end %>
然后我在我的布局的底部我所有的JavaScript load像这样
<%= javascript_include_tag 'jquery-1.3.2.js' %>
<script type="text/javascript">
$(function() {
<%= yield :dom_ready %>
);
</script>
另请参阅:http://stackoverflow.com/questions/18447186/rails-link-to-do-something-after-confirmation/18449331 – Yarin 2013-08-26 18:38:07