Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

content_tag isn't rendering blocks correctly

#4710

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.

Reported by Jonathan Hicks · May 26th, 2010 @ 05:33 PM

State: invalid
Milestone: 3.0.2
Assigned to: Mikel Lindsaar Mikel Lindsaar
Importance: Low

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • 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

    May 27th, 2010 @ 03:58 AM

  2. Rohit Arondekar
    Rohit Arondekar

    I've attached a failing test for this issue.

    May 27th, 2010 @ 04:13 AM

  3. Santiago Pastorino
    Santiago Pastorino
    • 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.

    May 27th, 2010 @ 04:54 AM

  4. Rohit Arondekar
    Rohit Arondekar

    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

    May 27th, 2010 @ 05:14 AM

  5. Jonathan Hicks
    Jonathan Hicks

    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
    

    May 27th, 2010 @ 05:20 PM

  6. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • 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
    

    May 29th, 2010 @ 08:18 PM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM