How to add confirm message with link_to Ruby on rails

后端 未结 10 1609
[愿得一人]
[愿得一人] 2020-12-29 17:45

I wanted to add confirmation message on link_to function with Ruby.

= link_to \'Reset message\', :action=>\'reset\' ,:confirm=>\'Are you sure?\'


        
相关标签:
10条回答
  • 2020-12-29 18:25
    <%= link_to 'Reset Message', data: {confirm:"Are you sure?"} %>
    

    remember to add the path, between 'reset message' and data

    0 讨论(0)
  • 2020-12-29 18:25

    Somehow does not work those code only Safari browser So I was involved button...

    <%= button_to('', delete_path(), method: "delete", data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>
    
    0 讨论(0)
  • 2020-12-29 18:27

    First, we need to understand what Js package respond to this kind of alerts in rails application. So for this, jquery_ujs package is reponsible for showing the alerts in rails.

    So you must have jquery & jquery_ujs in your application.js file.

    //= require jquery
    //= require jquery_ujs
    

    Now, we need to confirm, that application.js file is included in your required layout or not. By default layout file remains in application.html.erb in layout folder of views.

    <%= javascript_include_tag 'application' %>
    

    Next the link should have data-confirm & data-method attributes as

    <a href="/message/1/reset" data-method="delete" data-confirm="Are you sure?">
    

    In erb, this can be written as,

    = link_to 'Reset', message_path(@message), data: {method: 'delete', confirm: 'Are you sure?'}
    

    This should work if everything is aligned in same fashion.

    0 讨论(0)
  • 2020-12-29 18:29

    watch this railscasts video for better understanding.

    http://railscasts.com/episodes/205-unobtrusive-javascript

    rails documentation for link_to helper.

    http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

    0 讨论(0)
提交回复
热议问题