我正在尝试重用我编写的提供面板样式的html组件。类似于:
<div class="v-panel">
<div class="v-panel-tr"></div>
<h3>Some Title</h3>
<div class="v-panel-c">
.. content goes here
</div>
<div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
</div>所以我看到render需要一个块。我想我可以这样做:
# /shared/_panel.html.erb
<div class="v-panel">
<div class="v-panel-tr"></div>
<h3><%= title %></h3>
<div class="v-panel-c">
<%= yield %>
</div>
<div class="v-panel-b"><div class="v-panel-br"></div><div class="v-panel-bl"></div></div>
</div>我想做一些类似的事情:
#some html view
<%= render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %>
<p>Here is some content to be rendered inside the panel</p>
<% end %>不幸的是,这不适用于这个错误:
ActionView::TemplateError (/Users/bradrobertson/Repos/VeloUltralite/source/trunk/app/views/sessions/new.html.erb:1: , unexpected tRPAREN
old_output_buffer = output_buffer;;@output_buffer = ''; __in_erb_template=true ; @output_buffer.concat(( render :partial => '/shared/panel', :locals => {:title => "Welcome"} do ).to_s)
on line #1 of app/views/sessions/new.html.erb:
1: <%= render :partial => '/shared/panel', :locals => {:title => "Welcome"} do -%>
...所以它显然不喜欢带有块的=,但是如果我删除它,它就不会输出任何东西。
有没有人知道如何实现我想要实现的目标?我想在我的网站上的许多地方重新使用这个面板html。
发布于 2017-05-22 19:18:33
根据公认的答案,这就是我使用Rails4工作得很好的原因。
我们可以这样呈现一个面板:
= render_panel('Non Compliance Reports', type: 'primary') do
%p your content goes here!

这需要一个helper方法和一个共享视图:
帮助方法(ui_helper.rb)
def render_panel(heading, options = {}, &block)
options.reverse_merge!(type: 'default')
options[:panel_classes] = ["panel-#{options[:type]}"]
render layout: '/ui/panel', locals: { heading: heading, options: options } do
capture(&block)
end
end视图(/ui/panel.html.haml)
.panel{ class: options[:panel_classes] }
.panel-heading= heading
.panel-body
= yieldhttps://stackoverflow.com/questions/2951105
复制相似问题