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