rails link_to :remote

有些话、适合烂在心里 提交于 2019-11-29 20:07:57

You can bind to ajax calls like this:

<%= 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();
});

You don't need to combine the two. Just use the format.js to call the javascript.

In your controller:

Controller

 def my_method
     #code here
     respond_to do |format|
      format.js  {}
     end
    end

my_method.html.erb

<div id = "link-delete"></div>

my_method.js.erb

$("#link-delete").html("<%= escape_javascript(render(:partial => "text_message"))%>");

_text_message.html.erb

<p>Loading...</p>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!