Rails :confirm modifier callback?

前端 未结 3 472
遥遥无期
遥遥无期 2021-02-08 01:54

I want to call a javascript function that will be conditional upon a user\'s response to the confirm box.

For example, I have the following anchor:

<%         


        
3条回答
  •  暖寄归人
    2021-02-08 02:03

    I had a similar issue (How to use inline :confirm option for html helpers with AJAX calls?). @Micah's answer, even though works, is not what "Rails 3 way" would be.

    1. remote: true option needs to be added to the link_to helper, e.g.

    link_to 'Sign Out', destroy_session_path, remote: true, confirm: 'Are you sure that you would like to sign out?', method: :delete

    1. In the controller change respond_to block to answer to js

    2. create a js file with the name that corresponds to your method, e.g. destroy_session.js.coffee

    app/views/competitions/destroy_session.js.coffee:

    jQuery ->
      $("form[data-remote]").on "ajax:success", (e, data, status, xhr) ->
        $(e.currentTarget).closest('tr').fadeOut()`
    

    Hope that helps

提交回复
热议问题