This project is archived and is in readonly mode.

#4710 ✓invalid
Jonathan Hicks

content_tag isn't rendering blocks correctly

Reported by Jonathan Hicks | May 26th, 2010 @ 05:33 PM | in 3.0.2

This is what I've got:

  def panel(title = ' ', &block)
    content_tag(:div, :class => "panel") do
      content_tag(:div, title, :class => "header")
      content_tag(:div, capture(&block), :class => "contents contents-non-table")
    end
  end

The title div never gets rendered. It looks like the content_tag is only capturing the last thing returned in the block.

Comments and changes to this ticket

  • Rohit Arondekar

    Rohit Arondekar May 27th, 2010 @ 03:58 AM

    • Assigned user set to “Mikel Lindsaar”

    Confirmed on Rails master on 1.9.2-head in rails console. Output is in a pastie => http://pastie.org/979318

  • Rohit Arondekar

    Rohit Arondekar May 27th, 2010 @ 04:13 AM

    I've attached a failing test for this issue.

  • Santiago Pastorino

    Santiago Pastorino May 27th, 2010 @ 04:54 AM

    • Milestone cleared.
    • Tag changed from rails 3.0, content_tag to rails 3.0, content_tag, patch, test

    I'm a bit sleepy but seems that you miss the concat ... test case attached.

  • Rohit Arondekar

    Rohit Arondekar May 27th, 2010 @ 05:14 AM

    Thanks (again) Santiago. Jonathan you have to use concat inside the content_tag block, so your function should be:


    def panel(title = ' ', &block)

    content_tag(:div, :class => "panel") do
      concat content_tag(:div, title, :class => "header")
      concat content_tag(:div, capture(&block), :class => "contents contents-non-table")
    end
    

    end

  • Jonathan Hicks

    Jonathan Hicks May 27th, 2010 @ 05:20 PM

    Ok, that's fine but I don't think this example matches the one given
    in the video on railsdispatch.com. I believe Yahuda's example was for
    a sidebar and didn't accept a block, but it was very similar to my
    example. Something like:

    def sidebar()
      content_tag(:ul, :class => "sidebar") do
        content_tag(:li, "Welcome")
        content_tag(:li, "Home")
      end
    end
    
  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) May 29th, 2010 @ 08:18 PM

    • State changed from “new” to “invalid”

    In the outside-ERB form, you need to return a String from the helper, so you would do:

      def panel(title = ' ', &block)
        content_tag(:div, :class => "panel") do
          content_tag(:div, title, :class => "header") <<
          content_tag(:div, capture(&block), :class => "contents contents-non-table")
        end
      end
    
  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
    • Importance changed from “” to “Low”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages