Rails使用块进行部分渲染

我正在尝试重新使用我提供的面板样式的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> 

所以我看到渲染占用一个块。 我想我可以做这样的事情:

 # /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。

尽pipe上述两个答案都是正确的(不pipe怎么说,托尼都是​​这样的例子),但我最终find了上面这个post中最简洁的答案(Kornelis Sietsma评论)

我想render :layout 正是我所期待的:

 # Some View <%= render :layout => '/shared/panel', :locals => {:title => 'some title'} do %> <p>Here is some content</p> <% end %> 

结合:

 # /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> 

这是基于以前的答案的替代scheme。

shared/_modal.html.erb上创build你的部分shared/_modal.html.erb

 <div class="ui modal form"> <i class="close icon"></i> <div class="header"> <%= heading %> </div> <div class="content"> <%= capture(&block) %> </div> <div class="actions"> <div class="ui negative button">Cancel</div> <div class="ui positive button">Ok</div> </div> </div> 

application_helper.rb上定义你的方法:

 def modal_for(heading, &block) render( partial: 'shared/modal', locals: { heading: heading, block: block } ) end 

从任何视图调用它:

 <%= modal_for('My Title') do |t| %> <p>Here is some content to be rendered inside the partial</p> <% end %> 

您可以使用捕捉帮助程序,甚至可以在渲染调用中内联:

 <%= render 'my_partial', :locals => { :title => "Some Title" }, :captured => capture { %> <p>Here is some content to be rendered inside the partial</p> <% } %> 

并在共享/面板中:

 <h3><%= title %></h3> <div class="my-outer-wrapper"> <%= captured %> </div> 

这将产生:

 <h3>Some Title</h3> <div class="my-outer-wrapper"> <p>Here is some content to be rendered inside the partial</p> </div> 

http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html

我认为这将工作(只是做了快速的脏testing),如果你先将它分配给一个variables,然后输出它。

 <% foo = render :partial => '/shared/panel', :locals =>{:title => "Some Title"} do %> <p>Here is some content to be rendered inside the panel</p> <% end %> <%= foo %> 

基于接受的答案,这是什么对我来说使用Rails 4很好。

我们可以像这样渲染一个面板:

 = render_panel('Non Compliance Reports', type: 'primary') do %p your content goes here! 

在这里输入图像说明

这需要一个辅助方法和一个共享视图:

辅助方法(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 = yield