我对使用 Rails 3.2 并为 Bootstrap modals 制作助手感到沮丧。我不明白何时需要 concat 与何时不需要 concat 有时我最终会丢失标签,有时我会得到一个哈希,其中包含之前和结束标签之间的所有选项。当我在任何带有 do-end 的内容标签上使用 concat 时,一切都变得混乱了。我想做的就是复制这个html:
<div id="stupid_modal" class="modal hide fade" tabindex="-1" data-width="760">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fontello-icon-cancel-1"></i></button>
<h4>Modal header</h4>
</div>
<div class="modal-body">
<div class="page-header">
<p>Test header 1 2 3.</p>
</div>
<div class="row-fluid">
content here... blah blah
</div>
</div>
<div class="modal-footer">
<button type="button" data-dismiss="modal" class="btn">Close</button>
<button type="button" class="btn btn-green">Save changes</button>
</div>
</div>
我一辈子都无法让模态标题中的 h4 中的按钮正常工作。我也不能让页眉出现在模态正文中。
我的助手看起来像这样:
module ModalHelper
def modal(css_id, header_text, hidden = true, options = {},&block)
class_text = "modal"
class_text += " hide fade" if hidden
content_tag(:div, :class => 'modal hide fade', :id => css_id, :style => ("display:none;" if hidden)) do
concat modal_header(header_text)
concat modal_body(&block)
concat modal_footer
end
end
def modal_button(link_text, href)
modal_caller link_text, href, :button
end
def modal_link(link_text, href)
modal_caller link_text, href
end
private
def modal_caller(link_text, href, type = nil)
options = { :"data-toggle" => "modal" }
options.merge!({ :class => "btn" }) if type == :button
link_to link_text, "#" + href, options
end
def modal_header(header_text)
content_tag(:div, :class => 'modal-header') do
concat content_tag(:button,(content_tag(:i, :class => 'fontello-icon-cancel-1')),:class => 'close', :"data-dismiss" => 'modal', :"aria-hidden" => 'true')
concat content_tag(:h4, header_text)
end
end
def modal_body(page_header = "")
content_tag(:div, :class => 'modal-body') do
content_tag(:div, :class => 'page-header') do
concat content_tag(:p, page_header)
end
content_tag(:div, :class => 'row-fluid') do
yield
end
end
end
def modal_footer
content_tag(:div, :class => 'modal-footer') do
concat content_tag(:button, 'Close', type: "button", :class => 'btn btn-boo', :"data-dismiss" => 'modal')
concat content_tag(:button, 'Save', type: "button", class: 'btn btn-green')
end
end
结尾
链接如下所示:
<%= modal_link "New Stupid Modal", "stupid_modal" %>
模态 html 看起来像这样:
<%= modal('stupid_modal', 'Shouldnt this work?', submit: true, tabindex: '-1') do %>
<% render 'stupid_modal_partials/stupid_modal' %>
<% end %>
输出是这样的:
<button aria-hidden="true" class="close" data-dismiss="modal"><i>{:class=>"fontello-icon-cancel-1"}</i></button>
在页面源代码中看起来像这样:
<i>{:class=>"fontello-icon-cancel-1"}</i>
更新:
将 modal_header 更改为此有效:
def modal_header(header_text)
content_tag(:div, :class => 'modal-header') do
concat content_tag(:button,(content_tag(:i, "",:class => 'fontello-icon-cancel-1')),:class => 'close', :"data-dismiss" => 'modal', :"aria-hidden" => 'true')
concat content_tag(:h4, header_text)
end
end
但这不会:
def modal_header(header_text)
content_tag(:div, :class => 'modal-header') do
concat content_tag(:button,:class => 'close', :"data-dismiss" => 'modal', :"aria-hidden" => 'true') do
concat content_tag(:i, "",:class => 'fontello-icon-cancel-1')
end
concat content_tag(:h4, header_text)
end
end
这引出了一个问题,wzup 与 concat?我是否遗漏了什么——我还尝试将空引号作为按钮 content_tag 的第二个参数