Why does Rails use helper methods like link_to instead of <a href...>?

I am learning Rails, and I noticed that Rails constantly uses a helper method like link_to , and not just plain html. Now I can understand why they will use, they will use some helper methods, but I do not understand why they prefer helper methods only for simple html coding. Why does Rails prefer the helper method instead of writing html manually? Why did the Rails team make this design choice?

+4
source share
2 answers

In Rails applications, links are often generated using both methods for the URL and methods for the content, for example.

 <%= link_to @article.user.name, @article.user %> 

This is definitely more manageable than manually putting tags in <a> tags.

 <a href="<%= user_path(@article.user) %>"><%= @article.user.name %></a> 

(You use a router to create these URLs, right? What happens if you hard-label /users/1 and decide you want it to be /users/1-John-Doe later?)

However, for static links this is not a big deal.

 <a href="http://www.google.com/">Google</a> or <%= link_to 'Google', 'http://www.google.com/' %> 

One could use the case for the former for performance, or the latter for a consistent style, since both are equally manageable.

+10
source

If the routing URL for the model has changed, then the handwritten HTML will also be updated; by going through a function, the behavior (link to one of these things) is separated from the current routing scheme.

+7
source

All Articles