ruby-on-rails – 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。

虽然这两个答案上面的工作(很好的例子,tony链接到任何方面),我最后找到最简洁的答案在上面的帖子(评论由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>
http://stackoverflow.com/questions/2951105/rails-render-partial-with-block

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:ruby-on-rails – Rails渲染部分与块