I am back to ruby and rails after years of C# and asp .net.
I am struggling with basic problems, one of these is being able to delete records from an index view. It is a table, and each row is a record. In the last TD i need three icons that trigger show, edit and delete actions for that record. Show and edit actions are coded with link_to, while for the DELETE action I use
Now I would like to display a “trash” icon instead of the “Delete” text …
… and it seems this is rocket science.
Is there anyone that can point me in the right direction before loosing days googling around without any result?
Thanks a lot
Option 2: Install the font-awesome-rails gem, then replace your button_to statement with something like this (where obj is the object that will be deleted):
As the documentation shows in the examples, button_to accepts a block where any markup can be provided instead of using a text label. For example:
<%= button_to keyword, method: :delete, data: { turbo_confirm: "Are you sure?" } do %>
<svg...>....</svg>
<% end %>
Use whatever you want for the icon (SVG, image tag, etc) it’s just regular HTML markup and will show up inside the button tag.
Technically no, but JS can help here.
@djmolny’s suggestion is the right idea although it is dated. Back when Rails was using UJS the method option would be added as a data attribute which UJS would then pick up on.
Turbo has now superseded UJS (among other things). As part of that you now just assign the data attribute directly (i.e. data: { turbo_method: :delete }). With this Turbo will see that data attribute and when clicked will send a DELETE request instead of a GET request.
That being said I see no reason to do this. The link_to option offers similar API as button_to. Either you provide a pure text label as the first argument or you provide markup in the block with both helpers. I would much prefer button_to as it means no JS is required (UJS or Turbo). This is simpler and safer IMHO.
Some people will advocate link_to instead of button_to to avoid nested forms (which are invalid in HTML). But in that case, I would probably advocate a regular submit button and use the form attribute to avoid the nested form.