5

Should I be using the content_tag helper for all html tags when working with Rails?

Is it The Rails Way to use content_tag for even simple things like Header tags?

<%= content_tag :h2, :class => "bla bla" do %>
  Header
<% end %>

vs.

<h2>Header</h2>

Clearly just using straight html is much 'simpler' and 'shorter', but what is the correct Rails Way of doing things?

ardavis
  • 9,842
  • 12
  • 58
  • 112
  • There is a good reason (I think) why you would use that helper: when you are writing your own tag helper. – jgomo3 Oct 31 '22 at 14:25

3 Answers3

11

Using content_tag when you don't have to is a waste. There's no need to use ERBisms to generate static HTML so don't do it. If some other piece of code determines what tag to use, then you'd use content_tag to construct that tag.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
3

If you are asking the rails way of doing this, then its defiantly using 'content_tag', but using tag_helpers has its own advantages and disadvantages

Personally for me I can see these things, (Using rails helpers instead of pure HTML)

Advantages

1 - Your code will be cleaner. (with less lines)

2 - You will have more control other the elements. Ex: You can have your own helper tags like 'big_text_box' which will return a text box more than the normal with and you can use it across all the site

3 - You will be able to add attributes like class, id dynamically in the runtime

Disadvantages

1 - If you have a separate designer (I mean UI engineer) he/she will get confuse of by the code you have use. As its not pure html

2 - Its slow than the pure html (But this will not even noticeable unless otherwise your app is a damn major one...)

So its up to you to decide what to use, personally I prefer using rails helper tags as it makes me more comfortable

HTH

cheers

sameera

JVillella
  • 1,029
  • 1
  • 11
  • 21
sameera207
  • 16,547
  • 19
  • 87
  • 152
  • Thanks Sameera, I appreciate your input. I sure hope it becomes a major app :) Haha! I think I'll do a mix, depending on the type of tag. Everything you said makes perfect sense. – ardavis May 29 '11 at 05:27
2

One useful method is the "div_for", which is somewhat similar to the content_tag. If you find yourself marking up HTML elements with data you can reference later, "div_for" makes your life much easier.

Let's say you have a bunch of people being shown on a page and you need to wrap each with a div that has a unique ID so you can modify these elements with JS. By hand and straight HTML you would have to do:

<% @people.each do |p| %>
  <div id="person_<%= p.id %>"><%= p.name %></div>
<% end %>

That would get bothersome if you were doing LOTS of this with multiple attributes (I commonly use custom IDs, classes, and some data attributes). But using div_for you could write the above as:

<% @people.each do |p| %>
  <%= div_for(p) do %><%= @person.name %><% end %>
<% end %>

Makes the HTML a little easier to read when things get long and complex. I found it is much cleaner when working with javascript a lot.

http://apidock.com/rails/ActionView/Helpers/RecordTagHelper/div_for

Dan L
  • 4,319
  • 5
  • 41
  • 74