简体   繁体   中英

Ruby on Rails add confirmation to link_to

I'm building a Ruby on Rails application and I have generated some scaffold. I want to add a confirmation box (like the one that pops up when you delete an element) to a custom link I created.

The syntax for the element deletion is the following:

  <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>

This is my custom link:

   <td><%= link_to 'set done', :controller => 'users', :action => 'set_donation_done', :user => user, data: { confirm: 'Are you sure?' } %><td>

The problem is that the confirmation box on my custom link does not appear. If I inspect the element I see the following html code:

<a href="/set_donation_done?data%5Bconfirm%5D=Are+you+sure%3F&amp;user=71">set done</a>

What am I doing wrong?

Try with this:

<%= link_to 'set done', { controller: 'users', action: 'set_donation_done' }, user: user, data: { confirm: 'Are you sure?' } %>

Remember:

link_to(body, url_hash, html_hash)

http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

You don't need the controller action hash. Simply show when the HTML hash starts like so: <td><%= link_to 'Destroy', user, method: :delete,{ data: { confirm: 'Are you sure?' }} %></td> <td><%= link_to 'Destroy', user, method: :delete,{ data: { confirm: 'Are you sure?' }} %></td>

Found the solution:

 <td><%= link_to 'set done', {:controller => 'users', :action => 'set_donation_done', :user => user}, data: { confirm: 'Are you sure?'}  %><td>

Whit this formula I have my user data in params and I can see the confirmation box!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM