Ruby-on-rails rails link_to :remote
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15292270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails link_to :remote
提问by cgiacomi
I have the following:
我有以下几点:
<%= link_to my_path, method: :delete, confirm: 'Delete?', class: 'link-delete', 'data-message' => 'Are you sure?', 'data-severity' => 'danger', :remote => true do %>
<i class="icon-trash"></i>
<% end %>
which brings up a Bootstrap Modal for confirmation, and I wanted to hook onto the ajax call that so that I can display a spinner or some kind of text.
这会调出一个 Bootstrap Modal 进行确认,我想挂接到 ajax 调用上,这样我就可以显示一个微调器或某种文本。
I know that I can use unobtrusive javascript to listen to the click event like so, if I DON'T use ':remote => true' in my link_to
我知道如果我不在我的 link_to 中使用 ':remote => true',我可以使用不显眼的 javascript 来监听这样的点击事件
jQuery ->
$('.link-delete').live 'click', (event) ->
$('.link-delete').html("Loading...") #THE MSG OR ANIMATION I WANT TO DISPLAY
$.get(this.href, null, null, 'script')
false
but not sure how to combine the two when using ':remote => true'
但不确定在使用 ':remote => true' 时如何将两者结合起来
Any suggestions?
有什么建议?
thanks for the help
谢谢您的帮助
回答by Arjan
You can bind to ajax calls like this:
您可以像这样绑定到 ajax 调用:
<%= link_to my_path, method: :delete, confirm: 'Delete?', class: 'link-delete', 'data-message' => 'Are you sure?', 'data-severity' => 'danger', :remote => true do %>
<i class="icon-trash"></i>
<% end %>
$('.link-delete').bind('ajax:beforeSend', function() {
$('#mySpinner').show();
});
$('.link-delete').bind('ajax:complete', function() {
$('#mySpinner').hide();
});
回答by My God
You don't need to combine the two. Just use the format.js to call the javascript.
您不需要将两者结合起来。只需使用 format.js 来调用 javascript。
In your controller:
在您的控制器中:
Controller
控制器
def my_method
#code here
respond_to do |format|
format.js {}
end
end
my_method.html.erb
my_method.html.erb
<div id = "link-delete"></div>
my_method.js.erb
my_method.js.erb
$("#link-delete").html("<%= escape_javascript(render(:partial => "text_message"))%>");
_text_message.html.erb
_text_message.html.erb
<p>Loading...</p>

