2011-01-24 4 views
0

私は動的タブの作成のための簡単なタブヘルパーを作成しません。 form_forのレールヘルパーのようなもの。ruby​​/railsはコンテンツの問題をブロックします

(例えば簡体字)今の私が持っているもの。ここ

:ビューで

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 %> 

すべてがタブのコンテンツを除き、[OK]を動作しますが何とかこのように接合され、

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" 

私は新しい人です。ルビーブロックは、あまり経験していないものです。

誰かが私に説明することができます、ここで何が起こっているのか、タブブロックのコンテンツをキャッチする方法はありますか?

私はルビーからこれを試してくださいルビーに1.9.2

おかげ

UPDATE

を使用する:

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ブロックにあります。 誰もがこれを手伝ってくれますか?

おかげ

答えて

1

あなたは、ブロックをよく見ると、あなたが捕獲され、これらすべての文字があることがわかります。

これは、いくつかの方法でアプローチすることができ、ここでは一つです:

<%= tabs_navigation do |tabs| %> 
    <% tabs.tab :tab1 do %>tab1<% end %> 
    <% tabs.tab :tab2 do %>tab2<% end %> 
    <% tabs.tab :tab3 do %>tab3<% end %> 
<% end %> 

私はこれが唯一の最初の改行ストリップになると思う。個人的に

<%= tabs_navigation do |tabs| %> 
    <% tabs.tab :tab1 do -%> 
     tab1 
    <% end %> 
    <% tabs.tab :tab2 do -%> 
     tab2 
    <% end %> 
    <% tabs.tab :tab3 do -%> 
     tab3 
    <% end %> 
<% end %> 

を、私はおそらくちょうどstrip()を呼びたいですただし、yieldによって返された値については

関連する問題