Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7432 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

content_tag does not accept block from helper

Reported by: Nobet Assigned to: core
Priority: normal Milestone: 1.2.4
Component: ActionPack Version: edge
Severity: normal Keywords: content_tag
Cc: bryanl

Description

When I try to do this from a helper:

def give_me_content()
 content_tag(:div, :class => obj.foo) do
  "hi"
 end
end

I get this error

undefined local variable or method `_erbout' for ...

It seems that content_tag is not accepting a block from a helper. Having it accept a block would be useful if you have a helper that needs to nest several blocks (say, a link inside a span inside a div). It looks much cleaner to have a block for each level than to nest content_tag calls in the paramters, or to do a string with substitutions.

Attachments

content_tag_accepts_block_from_helper.diff (1.7 kB) - added by cody on 04/13/07 06:39:21.
tag_helper_allowing_content_tag_in_helpers.diff (1.4 kB) - added by biocs_nerd on 04/25/07 02:42:24.
Patch described by biocs_nerd

Change History

(in reply to: ↑ description ; follow-up: ↓ 2 ) 02/01/07 00:25:48 changed by dylans

note that this does jive in rel_1-1-0 fwiw.

(in reply to: ↑ 1 ) 03/12/07 21:53:31 changed by biocs_nerd

  • version changed from edge to 1.2.2.
  • milestone changed from 1.x to 1.2.3.

Replying to dylans:

note that this does jive in rel_1-1-0 fwiw.

This issue still exists. Fortunately the following fix was blogged by Agile Tortoise:

  def content_tag(name, content_or_options_with_block = nil, options = nil, &block)
    if block_given?
      options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
      content = block.call
    else
      content = content_or_options_with_block
    end
    content_tag_string(name, content, options)
  end

03/16/07 20:21:35 changed by bryanl

  • cc set to bryanl.

04/13/07 06:39:21 changed by cody

  • attachment content_tag_accepts_block_from_helper.diff added.

04/13/07 06:41:01 changed by cody

I also ran across this problem in Rails 1.2.3. I dropped in the fix from Agile Tortoise, which looks correct, and changed the unit tests correspondingly.

04/25/07 00:54:05 changed by biocs_nerd

I was wrong about the above fix. It works for helpers, but when used in views it doesn't have the desired effect.

04/25/07 02:42:00 changed by biocs_nerd

  • version changed from 1.2.2 to edge.

Okay, I have a patch with tests. This should work. I even built a test app to make sure this patch worked.

04/25/07 02:42:24 changed by biocs_nerd

  • attachment tag_helper_allowing_content_tag_in_helpers.diff added.

Patch described by biocs_nerd

05/02/07 22:31:22 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

Closed by [6652].