Rails content_tag helper for simple things?

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

Is the Rails method using content_tag even for simple things like header tags?

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

against.

<h2>Header</h2>

Clearly, just using direct html is much “simpler” and “shorter,” but what is the proper way for Rails to do things?

+3
source share
3 answers

Use content_tagwhen you do not need it is waste. There is no need to use ERBisms to generate static HTML, so don't do this. If any other piece of code determines which tag to use, then you should use content_tagto create that tag.

+10

, "content_tag", tag_helpers

( HTML)

< >

1 - . ( )

2 - .   . , "big_text_box", , .

3 - class, id

1 - ( -), . html

2 - , html ( , ...)

, , - rails, .

sameera

+3

- "div_for", content_tag. , HTML- , , "div_for" .

, , , , , JS. HTML :

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

It would be annoying if you would do LOTS of this with multiple attributes (I usually use custom identifiers, classes, and some data attributes). But using div_for, you can write above:

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

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

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

+1
source

All Articles