Rails' concat method and blocks with do…end doesn't work

霸气de小男生 提交于 2019-12-05 08:58:21

It comes down to Ruby's scoping. With concat content_tag :strong do "hello" end, the block is passed to concat, not to content_tag.

Toy around with this code and you'll 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 up custom Rails helpers" (http://thepugautomatic.com/2013/06/helpers/)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!