How to add confirm message with link_to Ruby on rails

后端 未结 10 1702
[愿得一人]
[愿得一人] 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:16

    Try this:

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

    or to be more clear

    = link_to('Reset message', {:action=>'reset'}, {:confirm=>'Are you sure?'})
    

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

    You will see that there are 3 parameters, when you are giving url as options like {:action => ..., :controller => ...}

    link_to(body, url_options = {}, html_options = {})
    

    In ruby, if the last parameter in a function call is a hash, you need not wrap it in {} characters (in other words, you can omit that in case, if the hash is the last parameter), so the code you have provided will be interpreted as a function call with only 2 parameters, 'Reset message' string and {:action=>'reset', :confirm=>'Are you sure?'} hash and the :confirm=>'Are you sure?' will be interpreted as a url_option instead of a html_option

提交回复
热议问题