steven_noble
steven_noble

Reputation: 4203

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:

%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. I'm not. I'm trying to call my partial directly.

What's going on?

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

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.

Upvotes: 1

Views: 293

Answers (2)

HashRocket
HashRocket

Reputation: 798

Try to remove 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. Partial should always start with underscore.

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

Upvotes: 0

Zaid Qureshi
Zaid Qureshi

Reputation: 1213

Try doing, the problem might be in locating the snippets directory

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

Upvotes: 1

Related Questions