Locals do not appear in partial unless collection is explici

Maybe I’m misunderstanding something but it appears as if
<%= render :partial => @items, @locals => { :this => ‘that’ } %>
does not give the local variable this but this one does:
<%= render :partial => ‘items/item’, :collection => @items, @locals => {
:this => ‘that’ } %>
Shouldn’t they mean the same thing?
I’m running rails 2.0.2

Martin

Martin Lübcke wrote:

Maybe I’m misunderstanding something but it appears as if
<%= render :partial => @items, @locals => { :this => ‘that’ } %>

Don’t you want :locals => { …
?

Mark B. wrote:

Martin Lübcke wrote:

Maybe I’m misunderstanding something but it appears as if
<%= render :partial => @items, @locals => { :this => ‘that’ } %>

Don’t you want :locals => { …
?

Yes, that is both what I want and what I mean. It should be :locals in
both lines, the @ was just a typo.

Martin

On 23 Mar 2008, at 17:46, Martin Lübcke wrote:

Maybe I’m misunderstanding something but it appears as if
<%= render :partial => @items, @locals => { :this => ‘that’ } %>
does not give the local variable this but this one does:
<%= render :partial => ‘items/item’, :collection => @items, @locals
=> {
:this => ‘that’ } %>

Is @locals just a typo? Should be :locals.

Fred

On 23 Mar 2008, at 18:58, Frederick C. wrote:

Is @locals just a typo? Should be :locals.

Oops, ignore me, being a bit slow today.

Fred

Martin Lübcke wrote:

<%= render :partial => @items, @locals => { :this => ‘that’ } %>

Yes, that is both what I want and what I mean. It should be :locals in
both lines, the @ was just a typo.

ok :slight_smile:

Looking at the code for render_partial (abbreviated):

def render_partial(partial_path, object_assigns = nil, local_assigns =
nil)
case partial_path
when String, Symbol, NilClass
:
local_assigns = local_assigns ? local_assigns.clone : {}
:
render("#{path}/_#{partial_name}", local_assigns)
:
when Array, ActiveRecord::Associations::AssociationCollection,
ActiveRecord::Associations::HasManyThroughAssociation
:
render_partial_collection(path, collection, nil,
object_assigns.value)
:

So, if you pass an array or an association collection (such as @items
above) as the value of the :partial key, then local_assigns is not
passed through. Unless I’m reading this all wrong.

I have not looked to see if this is expected behaviour or a bug…

Mark B. wrote:

def render_partial(partial_path, object_assigns = nil, local_assigns =
nil)
:
render_partial_collection(path, collection, nil,
object_assigns.value)
:

To complete the picture here, the definition of render goes:

def render(options = {}, old_local_assigns = {}, &block) #:nodoc:
:
elsif options.is_a?(Hash)
:
elsif options[:partial] && options[:collection]
render_partial_collection(options[:partial], options[:collection],
options[:spacer_template],
options[:locals])
elsif options[:partial]
render_partial(options[:partial],
ActionView::Base::ObjectWrapper.new(options[:object]),
options[:locals])

So yes, things work differently for partials if a collection is
supplied. Note that options[:locals] is supplied to #render_partial but
the argument in my previous post shows why this is not passed through to
#render_partial_collection, but it is if options[:collection] is
defined.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs