0

Does the render partial function take any type of collection? I tried passing a Set (@dogs) in and it doesn't seem to work:

<%= render(:partial => "dog", :collection => @dogs, :as => :dog) %>  

I tried looking it up on the docs http://guides.rubyonrails.org/layouts_and_rendering.html but it doesn't seem to suggest any limitation on sets... Is there something I have missed or another way to find out?

Thanks.

PS But when I tried to convert the very set to an array and it worked.

anNA
  • 330
  • 1
  • 2
  • 9

2 Answers2

1

It won't work with a set, because the PartialRenderer#collection method attempts to coerce the collection to an array using to_ary:

def collection
  if @options.key?(:collection)
    collection = @options[:collection]
    collection.respond_to?(:to_ary) ? collection.to_ary : []
  end
end

However Sets do not implement to_ary, so, as you have found, you must pass the collection like this:

:collection => @dogs.to_a
Andrew Haines
  • 6,574
  • 21
  • 34
  • would you say basically dont use sets as most ruby code only knows how to deal with hash/array? – anNA May 10 '13 at 15:45
-1

The proper way is to pass collection to a partials is

<%= render partial: "cat", locals: {cats: @cats} %>

RQ Bukhari
  • 167
  • 3
  • 8
  • That is the correct way to pass a whole collection to a partial. However, `render partial: "cat", collection: @cats` renders the partial once for each object in the collection, which is a whole different thing. – Andrew Haines Feb 04 '13 at 15:44