Я хочу создать простой помощник вкладок для создания динамических вкладок. Что-то вроде 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
Спасибо
UPDATE
я пробую это от 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
конец это работает как положено. Проблема / ошибка в блоках вида / erb.
Кто-нибудь может мне помочь с этим?
спасибо