【发布时间】:2011-06-14 17:50:14
【问题描述】:
我不会为动态标签创建创建简单的标签助手。 form_for rails helper 之类的东西。
这是我现在拥有的(例如简化):
class Tabs
attr_reader :tabs, :type, :subtype, :args
def initialize(*args)
@tabs = []
@type = args[0] || 'horizontal'
@subtype = args[1] || 'basic'
@args = args.extract_options!
end
def tab(*args, &block)
tab ={}
tab[:name] =args[0]
tab[:content]= capture(&block)
#same thing with with_output_buffer(&block)
args = args.extract_options!
tab = args.merge(tab)
@tabs << tab
end
end
def tabs_navigation(*args, & block)
tabs_constructor = Tabs.new(args)
capture(tabs_constructor, & block)
#iteration of tabs hash array and building tabs structure goes here
#tabs_constructor.tabs.each bla,bla
end
在视图中
<%= tabs_navigation do |tabs| %>
<% tabs.tab :tab1 do %>
tab1
<% end %>
<% tabs.tab :tab2 do %>
tab2
<% end %>
<% tabs.tab :tab3 do %>
tab3
<% end %>
<% end %>
一切正常,除了选项卡的内容以某种方式加入如下:
content for tab1 is: :content=>"\n tab1\n"
content for tab2 is: :content=>"\n tab1\n tab2\n"
content for tab3 is: :content=>"\n tab1\n tab2\n tab3\n"
我是新手,红宝石块是我没有太多经验的东西。
谁能给我解释一下,这里发生了什么以及如何捕捉标签块的内容?
使用 ruby 1.9.2
谢谢
更新
我用 ruby 试试这个:
class Foo
attr_reader :arr
def initialize
@arr = []
end
def bar
hash = {}
hash[:content] = yield
@arr << hash
end
end
def FooBars
foo = Foo.new
yield foo
puts foo.arr
end
FooBars do |fo|
fo.bar do
'bar1'
end
fo.bar do
'bar2'
end
end
结束它按预期工作。问题/错误在 rails 视图/erb 块中。 有人可以帮我解决这个问题吗?
谢谢
【问题讨论】: