Partial problem

def my_controller
@variable = [{‘myfield’=>‘grey’},{‘myfield’=>‘blue’}]
end

#-------------------------my_view

<%= render :partial=>'my_partial, :collection=>@variable %>

#----------------------_my_partial

<%= my_partial['myfield'] %>

I would have expected the view to print out grey, blue.
However the my_partial local variable in _my_partial comes out a nil
object.

Can anyone tell what i’m doing wrong?
thanks, simon.

You’ve set up @variable as an array of hashes and are trying to access
it in your partial as a simple hash. You need to make your mind up
whether you want an array, a hash, or something more complex.

Simon wrote:

def my_controller
@variable = [{‘myfield’=>‘grey’},{‘myfield’=>‘blue’}]
end

#-------------------------my_view

<%= render :partial=>'my_partial, :collection=>@variable %>

#----------------------_my_partial

<%= my_partial['myfield'] %>

I would have expected the view to print out grey, blue.
However the my_partial local variable in _my_partial comes out a nil
object.

Can anyone tell what i’m doing wrong?
thanks, simon.

D’oh! My mistake - what you are doing is fine. Your problem may be due
to a missing quote in your call to render :partial.

HTH, Mick.

Simon wrote:

thanks for the reply,
i’d like to access an array of hashes from my partial. Can i
do this using the collection parameter?
thanks.

thanks for the reply,
i’d like to access an array of hashes from my partial. Can i
do this using the collection parameter?
thanks.

My understanding of the render :partial syntax is that when you use
the :collection part, you pass an array to that argument and each
element in that array is passed to the parial called (my_partial) as a
local variable partial_name (my_partial)

So at this point, if you have an array of simple hashes then each time
my_partial runs it should run with a simple hash called my_partial

If my understanding is correct, then I think the code should run.

Can anyone clarify why this doesn’t work

Mick S. wrote:

D’oh! My mistake - what you are doing is fine. Your problem may be due
to a missing quote in your call to render :partial.

HTH, Mick.

no my syntax looks fine - in my actual code anyway.

Daniel ----- wrote:

Is there definitely data in the @variable before you call the partial?

Could you post your actual code?

Cheers

Yeah thought of that, the @variable has the array of hashes in it at the
view stage. The code above is exactly what I have test(with no missing
quote).

-Si.

So if in your view you put in an iterator instead of the call to
partial (Not permenantly, just for debugging this one thing!)

ie
render :text => @variable.collect { |v| “

my field =
#{p[‘myfield’]}

” }

Do you get any sensible output for this?

yeah i looped through the array, except i used a for loop like

<% for var in @variable %>
<%= var[‘myfield’] %>
<% end %>

-The output looks like what i expected.

Are you getting the same results? I’d just like to know
if i’m doing something wrong.

I’m at work at the moment, and I don’t have ruby installed on this box.

In your partial have a look at what local and instance variables are
available.

ie.

instance_variables.join(’
’)

local_variables.join(’
’)

Are the correct variables available?

thanks Daniel,
after display the local variables, it appears as though the keys of the
hash
are converted into variables directly. In this example, the local
variable ‘myfield’ becomes created in the partial as opposed to
accessing it through my_partial[‘myfield’] as i would have expected.

The my_partial local variable then becomes a nil object.

So much for the element of lease surprise :slight_smile:

Is there definitely data in the @variable before you call the partial?

Could you post your actual code?

Cheers

not sure if its a bug, it could be the way the render method
is written internally. Its just not very transparent though.

Daniel ----- wrote:

Thats a new one to me as well…

Is that a bug?

There is a patch in the system for this.

http://dev.rubyonrails.org/ticket/1557

I’d say it’s definitely a bug.

Thats a new one to me as well…

Is that a bug?

Daniel ----- wrote:

There is a patch in the system for this.

http://dev.rubyonrails.org/ticket/1557

I ran into this problem, also. with an array of hashes being passed. It
didn’t even seem to convert each has item into a local variable
correctly (some didn’t exist, some were nil). Snippet below

<%
blah = [
{:id => “564788”, :name => “test1”},
{:id => “3983dk”, :name => “test2”},
{:id => “56eke8”, :name => “test3”},
{:id => “ijks8”, :name => “test4”}
]
%>

<%= render :partial => ‘site_rows’ ,
:collection => blah
%>

The site_rows variable doesn’t show up as it is expected in the partial.
site_rows[:id] and site_rows[:name} definitely don’t show up.

Yes, I know this construct is pretty ugly (and I was just using it to
test a partial and design), but it will be nice if it is fixed.

Seems like this issue has come up a bunch of times in other posts
(Partial problem from down under - Rails - Ruby-Forum or
Partial problem - Rails - Ruby-Forum). The second post is
actually comically sad since people seemed more interested in critiquing
style than actually answering the guy’s question.

Cheers