Passing an array to a partial

I’m using a partial that is responsible for setting up a link_to_remote helper to navigate through files in a directory. I’m keeping track of the current index, and I need to pass the partial an array of filenames, so I can parse it and display the title with each prev/next navigation, based on the current index. However, I can’t seem to pass an array to the partial. I though using :object would work, but apparently not. Here’s what I have:

1
2
3
4
5
6
7
8
# the call to render the partial (passing in the @screens array)
<%= render :partial => 'navigation_link',
           :object => @screens,
           :locals => {:link_text     => 'Previous',
                       :action        => 'navigate_screen',
                       :total_screens => @screens.size,
                       :screen_index  => @index,
                       :screen_title  => @screens[@index]} %>

And here’s what I have in the partial…

1
2
3
4
5
6
7
8
9
10
# _navigation_link.rhtml
<%= link_to_remote link_text,
                   :url => {:controller   => 'courses',
                            :action       => 'navigate_screen',
                            :screen_count => total_screens,
                            :index        => screen_index,
                            :title        => @screens[screen_index], # this fails
                            :direction    => link_text.downcase},
                   :loading  => "Element.show('loading');",
                   :complete => "Element.hide('loading');" %>

Why would <%= @screens[screen_index] %> display the correct filename (yes, from within the partial) if it was nil? I’ve tried :collection as well, but I don’t think that’s what I want, here. Any thoughts on why I’m getting a nil.[] error even though it, apparently, isn’t nil? I’m open to other suggestions as well, but I’d rather not use the session.

Comments

01

Chris on Thu Feb 15 at 02:53PM

I don’t think this should even be a partial, but should simply be a helper method.

As far as @screens is concerned, it’s likely nil because view variables are not passed on, if I remember correctly. The data is in the partial, but will be accessible by the name of the partial, in this case navigation_link I believe. So, navigation_link is essentially the variable @screens.

02

Ryan on Thu Feb 15 at 07:19PM

Yeah, you’re right (again) about this needing to be a helper method. I think I’ll do that in the morning. It started out much bigger, where a partial would have made more sense, but then I realized a few unnecessary things that were going on, which allowed me to get rid of a couple of actions, leaving only the need for this link. And I left it there.

Sometimes I guess I get caught up in the moment, and don’t stop to re-think things until later, which is probably why they say it’s good to walk away from it for a little bit. Thanks for the tip, and you were right about the navigation_link variable.

Have something to say?
Please rewrite the image text Are You Human? Hint: Are You Human? Formatting Tips

or

© 2009 Ryan Heath | Site Management A Ruby on Rails production.

Get in Touch