Links vs Buttons
Now that I’m writing more websites, I’m trying to make them better. A lot of it has to do with making them look better so that people have a better (UX) user experience. I’ve been using bootstrap more, which is nice. But now I need to get into more nuts and bolts stuff.
The question then is, when do you use a link and when do you use a button. Links are easy. If something is just navigation, use a link. If I’m looking at a page and want to move to the edit page, I should have a link, probably called edit. Buttons are for things that would change the database behind the site.
A simpler way to think of the difference between the two is to use a link if that’s not really going to do any harm to the website. If there is a chance to do some damage to something, use a button.
By default, rails uses links for just about everything. Specifically, scaffolding defaults to using a link to delete an entry. Based on the above information, I should be using a button there. So what do I need to change.
Here’s what the basic delete link looks like:
<%= link_to 'Delete', applicant, method: :delete, data: { confirm: 'Are you sure?' } %>
To change this to a button, I’d user this:
<%= button_to "Delete", { action: :destroy, id: applicant.id }, method: :delete, data: { confirm: 'Are you sure?' } %>
Note in both of these lines, I have to specify that the method we want to use is delete. In rails, by default, a link_to method looks for /applicants/new (for example) using GET. The button_to method looks for /applicants/new using POST.