ruby / rails阻止内容问题

我不会创建简单的选项卡帮助动态选项卡创建。 类似于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            tab1n"
content for tab2 is: :content=>"n            tab1n            tab2n"
content for tab3 is: :content=>"n            tab1n            tab2n            tab3n"

我是新手,红宝石块是我没有太多经验的东西。

有人可以向我解释,这里发生了什么,以及如何捕捉标签块的内容?

使用红宝石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

结束它按预期工作。 问题/错误是在rails的视图/ erb块..任何人都可以帮助我呢?

谢谢


如果你仔细观察你的方块,你应该看到被捕获的所有角色都在那里。

这可以通过几种方式来解决,这里有一个:

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

就个人而言,我可能只是在由yield返回的值上调用strip()

链接地址: http://www.djcxy.com/p/25827.html

上一篇: ruby/rails block content problem

下一篇: How to convert a ruby hash object to JSON?