Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails tags vs HTML tags

I have a question about tags in .html.erb files.

Which practice is better: Ruby on Rails tags or HTML tags? Is it better to use Ruby on Rails tags as much as possible? Or maybe I should avoid them and choose HTML ones instead? How to decide?

like image 516
Daumantas Versockas Avatar asked Nov 19 '25 22:11

Daumantas Versockas


1 Answers

Using the Rails tag helpers will occur a pretty big overhead cost compared to simply writing a html tag in ERB (or Haml or Slim). This is usually an acceptable tradeoff for readablity and productivity when working with "dynamic html" (in the sense of markup created with server-side input) but is totally unnecessary if there is no interpolation going on.

Basically just use common sense - the Rails tag helpers (and all their derivatives such as the form helpers) are not meant as a total replacement for writing HTML in your views but rather as utilities to make it easier and cleaner to bind variables to the attributes and content of tags. They also make it much easier to write your own helper methods where you programatically create HTML instead of using string concatenation.

TLDR; Using both where applicable is the best practice.

like image 161
max Avatar answered Nov 22 '25 15:11

max