2

UPDATE - I just checked and NEITHER confirm is working.

I need to have a confirm on a link_to. I've tried a couple of variations on the data/confirm attribute but it is bypassing the confirm dialog.

My link is

 <%= link_to 'new bs line', 
   :controller => :bedsheet_lines, 
   :action => :create_new_bedsheet_line,  
   :data => {:confirm => 'Are you sure that you want to export the Bedsheets?'} %> <br>

That does not work, but a regular link_to does: and neither does this

  <%= link_to "Export Data as a CSV file ", 
      slitter_bedsheet_export_path(format: "csv"), 
      :data => {:confirm => 'Are you sure that you want to export the Bedsheets?'} %> <br>

If I do an inspect, the link that won't show the confirm looks like

  <a href="/bedsheet_lines/new_bedsheet_line?
   data%5Bconfirm%5D=Are+you+sure+that+you+want+to+export+the+Bedsheets%3F">
  new bs line</a> <br>

Whereas the link that does show a confirm looks like Nor dies this work.

  <a data-confirm="Are you sure that you want to export the Bedsheets?" 
   href="/csv_export.csv">Export Data as a CSV file </a> <br>

My suspicion is that the problem is that confirm will not work when your link_to specifies and action and controller. If that is true, I should be able to find a way to work around that. Now I'm not sure where my problem lies.

Chris Mendla
  • 987
  • 2
  • 10
  • 25

1 Answers1

4

Not sure why the hash syntax matters but I use

<%= link_to 'text', path(param), data: { confirm: 'confirmation' } %>

Edit:

Specifying controller and action it would be:

<%= link_to 'text', { controller: 'bedsheet_lines', action: 'create_new_bedsheet_line' }, data: { confirm: 'confirmation' } %>
Juan Fuentes
  • 1,743
  • 2
  • 20
  • 33
  • I was missing the brackets enclosing controller/action. Also, I found the a hint at http://stackoverflow.com/questions/16668949/how-to-add-confirm-message-with-link-to-ruby-on-rails – Chris Mendla Mar 28 '17 at 18:46
  • 1
    Apparently I had removed `//= require jquery_ujs` from application.js when I upgraded my Jquery libraries (Not sure exactly why I did that at that time). I put that line back in, made the changes you suggest, precompiled, restarted the server and the confirms are now working – Chris Mendla Mar 28 '17 at 18:47