The Rails concatenated method and do ... end blocks do not work

I just read about the Rails' method concatfor clearing helpers that output something here http://thepugautomatic.com/2013/06/helpers/ .

I played with it, and I found that it does not react the same way it locks curly braces and blocks with do ... end.

def output_something
  concat content_tag :strong { "hello" } # works
  concat content_tag :strong do "hello" end # doesn't work
  concat(content_tag :strong do "hello" end) # works, but doesn't make much sense to use with multi line blocks
end

I did not know that curly braces and ... end blocks seem to have different meanings. Is there a way to use concatwith do ... end without putting brackets around it (third example)? Otherwise, apparently, it is rather useless for certain situations, for example. when I want to concatenate UL with many LI elements in it, so I need to use a few lines of code.

+4
source share
1 answer

It comes down to watching Ruby. With the concat content_tag :strong do "hello" endblock passed to concat, not to content_tag.

Play with this code and you will see:

def concat(x)
  puts "concat #{x}"
  puts "concat got block!" if block_given?
end

def content_tag(name)
  puts "content_tag #{name}"
  puts "content_tag got block!" if block_given?
  "return value of content_tag"
end

concat content_tag :strong do end
concat content_tag :strong {}

Quote: Henrik N from "Using concat and capture to clean custom Rails helpers" ( http://thepugautomatic.com/2013/06/helpers/ )

+3
source

All Articles