Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3096 (closed enhancement: duplicate)

Opened 3 years ago

Last modified 1 year ago

Inconsistent (undocumented) behaviour of render :partial with :collection

Reported by: wejn@box.cz Assigned to: David
Priority: normal Milestone:
Component: ActionPack Version: 0.14.3
Severity: normal Keywords: actionpack render partial collection local_assigns
Cc: wejn@box.cz

Description

When rendering collection of partials there's special case if collection contains hashes:

_p.rhtml:

<%=h p.inspect %>

index.rhtml:

<%= render :partial => 'p', :collection => [1,2,3] %>
<%= render :partial => 'p', :collection => [{:a => 1, :b => 2}, {:c => 3, :d => 4}] %>

will output:

1 2 3 nil nil

although I expect it to output:

1 2 3 {:a => 1, :b => 2} {:c => 3, :d => 4}

The culprit's probably extracting_object method (in action_view/partials.rb) because the is_a?(Hash) triggers this behaviour.

It might be a feature (since keys of the hash are injected as local_assigns) ... but this feature is not mentioned in documentation.

Change History

12/04/05 21:06:45 changed by htonl

  • status changed from new to closed.
  • resolution set to invalid.

Taken from the API docs:

NOTE: Due to backwards compatibility concerns, the collection can’t be one of hashes. Normally you’d also just keep domain objects, like Active Records, in there.

12/04/05 22:14:56 changed by wejn@box.cz

  • status changed from closed to reopened.
  • resolution deleted.

Oh, interesting.

Now I found that notice after grepping through the whole source of actionpack.

Would it be possible to mention this thing in #render's description?

08/17/06 03:11:16 changed by anonymous

  • status changed from reopened to closed.
  • resolution set to duplicate.

duplicate of 5524

07/19/07 18:14:08 changed by epirogov

  • type changed from defect to enhancement.

ROR way of dealing with this backward compatibility limitation would be to use a class ( not < Hash) and place it into collection. Sample code if you need class like a Hash http://ep.blogware.com/blog/_archives/2007/7/19/3105080.html