简体   繁体   English

为什么Rails不会渲染我的部分内容

[英]Why won't Rails render my partial

I'm trying to DRY up my views a bit by turning multiple nested elements into partials that yield blocks. 我试图通过将多个嵌套元素转换为产生块的局部视图来稍微干燥一下我的视图。 I decided to start with a simple example to get going. 我决定从一个简单的例子开始。

In my view: 在我看来:

= render :partial => "../snippets/indented_divs", :locals => {:width => 12} do 
  %p foo

In app/views/snippets/_indented_divs.html.haml : app/views/snippets/_indented_divs.html.haml

%div.col-sm-1
%div{:class => "col-sm-#{width-2}"}
  = yield
%div.col-sm-1

The error: 错误:

'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

I've seen discussion of this error, but it relates to people trying to implicitly convert an ActiveModel-compatible object into a partial. 我已经看到了有关此错误的讨论,但它与试图将与ActiveModel兼容的对象隐式转换为部分对象的人们有关。 I'm not. 我不是。 I'm trying to call my partial directly. 我正在尝试直接致电我的部分。

What's going on? 这是怎么回事?

UPDATE UPDATE

BTW, the problem is clearly with yielding the block, not finding the partial, because when I update the view to not take the block... 顺便说一句,问题显然是产生块,而不是找到部分,因为当我更新视图以不采用块时...

= render :partial => "/_snippets/indented_divs", :locals => {:width => 12} 
%p foo

...I get... 我明白了

<div class='col-sm-1'></div>
<div class='col-sm-10'></div>
<div class='col-sm-1'></div>
<p>foo</p>

UPDATE 2 更新2

Turns out I can make this work with: 事实证明,我可以使用以下方法:

  = render :layout => "/snippets/indented_divs", :locals => {:width => 12} do
    %p foo

But as I'm actually rendering a partial here, it'd still be good to know why passing a block to a rendered partial didn't work. 但是,由于我实际上是在这里渲染部分,所以仍然很高兴知道为什么将块传递给渲染的部分不起作用。

Try doing, the problem might be in locating the snippets directory 尝试做,问题可能出在查找摘录目录中

= render "/_snippets/indented_divs", :width => 12 do 
%p foo

Try to remove undercore : 尝试删除undercore

= render :partial => "snippets/indented_divs", :locals => {:width => 12} do 
%p foo

UPDATE: 更新:

add underscore to the file, it should be _indented_divs.html.haml . 在文件中添加underscore ,应该为_indented_divs.html.haml Partial should always start with underscore . 部分内容应始终以underscore

app/views/snippets/_indented_divs.html.haml:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM