开发者

How to modify the :confirm or add a new method to link_to UrlHelper?

I use this code to delete record:

<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>

When I click the "Destroy", it will have an alert box prompt out and ask me to destroy or not. Can I modify the :confirm to an AJAX call? or can I add a :myConfirm in to the link_to tag to i开发者_StackOverflow中文版mplement my own :myConfirm? Thx.


Indeed you can. Create your own helper method (suggested) or extend the default link_to helper (not suggested) in order to include your custom behavior.

If you are wondering whether you can achieve the result simply passing an additional option, the answer is no. You actually need to code the AJAX call or the feature, then create the helper logic to handle the new behavior.

Best of all, avoid hacking the helper itself and create an unobtrusive JavaScript callback based, for instance, on a specific tag class. You can easily to this in jQuery

$("ajax-confirm").click(function(){
  // here send the AJAX request
  // and display the confirm dialog
  var confirm = true;  
  return confirm;
});

<%= link_to "...", "...", :class => "ajax-confirm" %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜