image

Sascha Depold

Engineering Manager

Blog > Render collections with layout in Rails

Render collections with layout in Rails

Today I found a very strange behavior of Ruby on Rails. I wanted to render a partial using the collection option. So far so normal. But: I needed the result to be wrapped with some stuff, so I found out, that you can just specify a layout! What I expected to get was something like:

Layout Layout Layout
<%= yield %> <—- this should be filled with my partial Layout Layout Layout

So this is what I executed:

my_array = [“hello”, “world”]
render(:partial => “test”, :collection => my_array, :layout => “test_layout”)

Layout file:

some text before yield
<%= yield %>
some text after yield

Partial file:

<%= test %>

I expected the result to be either:

some text before yield
hello
some text after yield
some text before yield
world
some text after yield

or:

some text before yield
hello
world
some text after yield

But instead I got this:

some text before yield
hello
world
some text after yield
some text before yield
hello
world
some text after yield

So please tell me… WTF is going on!!