Forum: Ruby on Rails Helper content_tag and Blocks

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5ac4033486edab2e9059ca1ccf13c8fa?d=identicon&s=25 Jordan McKible (jmckible)
on 2007-03-15 19:46
The content_tag helper allows you to do things like:

content_tag(:p, "Hello world!")
# => <p>Hello world!</p>

It can take in blocks, for functionality like this:

<% content_tag :div, :class => "strong" do -%>
   Hello world!
<% end -%>
# => <div class="strong"><p>Hello world!</p></div>

Unfortunately blocks only work in erb and not as helper methods.  This
problem already has a ticket http://dev.rubyonrails.org/ticket/7432

Greg Pierce has come up with a fix
http://greg.agiletortoise.com/2007/03/01/rails-con... but
I'm looking for more.

I'd like to write something along the lines of

content_tag :table do
  content_tag :tr do
    content_tag :td, "first"
    content_tag :td, "second"
  end
end

With Greg's fix, that code doesn't crash at least, but the block.call
line only returns the second td tag.  I generally understand what the
problem is, but my skills with Ruby blocks are coming up short.  Anyone
have an idea how to fix this?
351d6827001f290116032995a87ebb81?d=identicon&s=25 Greg Pierce (Guest)
on 2007-03-17 03:58
If you want to write HTML like that, you should look at <a
href="http://redhanded.hobix.com/inspect/markabyForRails....

greg.
This topic is locked and can not be replied to.