0
我想我已经为自己创建了一个令人讨厌的问题。请看下图:Rails 4:在方法和块中嵌套content_tag
查看
<% tl = tablist_initialize(params[:tab], 'profile') %>
# lots of html
# ...
<%= tl.tab_content do %>
<% tl.tab_panel 'profile' do %>
Hello!
<% end %>
<% tl.tab_panel 'settings' do %>
Sup!
<% end %>
<% tl.tab_panel 'notifications' do %>
Nothing!
<% end %>
<% end %>
助手
class TabList
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
attr_accessor :output_buffer
def initialize(tab, primary)
@current_tab = tab
@primary = primary
end
def tab_content(&block)
content_tag(:div, :class => "tab-content", &block)
end
def tab_panel(id, &block)
concat(content_tag(:div, {:class => (@current_tab == id ? "tab-pane active" : "tab-pane"),
:role => "tabpanel", :id => id }, &block))
end
# other methods
# ...
end
输出
我的计划在这里,是很容易地创建引导标签。结果现在是这样的:
Hello! Sup! Nothing!
<div class="tab-content">
<div class="tab-pane active" role="tabpanel" id="profile">
Hello!
</div>
<div class="tab-pane" role="tabpanel" id="settings">
Sup!
</div>
<div class="tab-pane" role="tabpanel" id="notifications">
Nothing!
</div>
</div>
为什么第一行结果(Hello!Sup!Nothing!)?我觉得奇怪的是输出内部块两次:一次正确,一次完全在任何块之外(在开始时)。我尝试了不同的方法:省略了concat函数,在第一个content_tag的yield处使用concat,但所有这些尝试都没有给出我希望的结果。
谢谢,但不幸的是也没有工作。 http://pastie.org/10739079 – maikovich
感谢您的更新,但这也没有办法。 http://pastie.org/10739099它仍然打印内部块内容多次,它发出与标签内容类的外部div。 – maikovich