Haml: link_to vs button_to

From what I understand, link_to used for get methods, and button_to used for post methods.

On the other hand, I was told that using semantics HTML5 <button> used for any type of clickable ... well, buttons. In case I have a button with a click that sends the user to the completed form, should I create button_to or link_to ?

+8
ruby-on-rails button hyperlink haml
source share
4 answers

It is easier than you think. These methods are Rails helpers and have nothing to do with haml. Yes, one method for get and another for post methods. If you need to send any data to the controller, use button_to (for example, when deleting a record). Otherwise link_to enough.

Alternatively, you can do link_to data placement using the :method parameter:

 = link_to "Something", some_path, :method => :post 

When answering your question, use link_to .

+15
source share

You must use links to indicate to the user a resource, such as an article.

But you should use the buttons to indicate the action (for example, β€œCreate” / β€œSubmit” on the edit page). If this is not consistent with your interface, use them as a link.

Here's why: you cannot direct your user to any action without GET via link_to if he lacks javascript support. Thus, buttons are the only options that can trigger your send / destroy action in this case.

You cannot use both approaches if your link points to a page that ultimately leads to a change in the resource (link / button on the edit / create page that shows the form), for example, in your case.

+3
source share

If you just want to send the user somewhere, this is a request for receipt. Therefore, in this case you should use link_to. By the way, you can use link_to for mail requests and other requests (e.g. button_to) if you specify a method. For example: =link_to "some path", some_path, :method => :get

+2
source share

The main difference between #link_to and #button_to is that #link_to simply creates a link A tag and makes a simple AJAX request without additional data, and #button_to creates a FORM with user data, so the form can be used to execute an extended AJAX request on the web server. These forms include a built-in CSRF token, which is used to authenticate the request. In the case of #link_to CSRF token must be serialized and sent to the on_click event.

0
source share

All Articles