竞赛/ show.html.haml:
- if modal
- content_for(:modal_header) do
= render 'contests/contest_header'
- content_for(:modal_body) do
= render 'contests/contest_body'
- else
= render 'contests/contest_header'
= render 'contests/contest_body'
modal
是一个布尔局部,表示视图是否在模态模板中呈现,而不是常规应用程序模板。模态具有content_for块,而主应用程序模板不具有:
布局/ modal.html.haml:
-modal = true
.modal-header
.row
.col-xs-11
= yield(:modal_header)
.col-xs-1
%button{'type': 'button', 'class': 'close', 'data-dismiss': 'modal', 'aria-label': 'close'}
%span{'aria-hidden': 'true'} ×
.modal-body
= render 'layouts/messages'
- if content_for?(:modal_body)
= yield(:modal_body)
- else
=yield
我正在寻找的东西可以在modal
为假时有条件地使content_for块禁用。而不是重复我的渲染语句。
答案 0 :(得分:0)
从另一个SO答案找到解决方案! https://stackoverflow.com/a/27398070/378622
这是我的实施
# app/helpers/application_helper.rb
def modal_content_for(name, &block)
if params[:modal] == '1'
content_for 'modal_' + name.to_s, nil, &block
else
capture_haml(&block)
end
end