0

I have a layered partial scheme and am trying to write DRY code. I need to pass a variable into a partial and maintain its array status so that within the partial I can render another partial with the :collection parameter.

on this page I render the 'feed' partial:

 %div.row
    %div.span6
      %h1 Posts 
      /Post feed
      =render 'feed',:locals => {:feed_items => @posts}
    %div.span6
      %h1 Groups
      /Group feed
      =render 'feed', :locals => {:feed_items => @groups}

Here is the 'feed' partial:

%ol
  -if feed_items.first.is_a?(Post)
    =render :partial => 'post_feed_item', :collection => feed_items
  -else
    =render :partial => 'group_feed_item', :collection => feed_items
=will_paginate feed_items

Currently gives me this error:

undefined local variable or method `feed_items' for #<#<Class:0x007fa8e2aa8b00>:0x007fa8e5405390>

UPDATE 1:

real error. final partial doesn't recognize component of the :collection passed to it:

undefined local variable or method `feed_item' for #<#<Class:0x007fa8e2aa8b00>:0x007fa8e2c6d670>

final partial 'post_feed_item'(layer 3?):

%li.feed_item.row-fluid
  %div.image.span3
    =link_to image_tag(feed_item.assets.empty?  ? '/assets/small.png': feed_item.assets.first.image.url(:small), :alt => feed_item.title), post_url(feed_item)

So there is some problem in the first partial...feed_items isn't being parsed as an array?

5
  • you need to show the partial where the error is. it looks like you are calling feed_item on one of your post_feed_item or group_feed_item partial which is not right. the convention is the local variable has the same name as the name of that partial Commented Feb 19, 2013 at 1:57
  • ok let me try changing the local var name Commented Feb 19, 2013 at 2:00
  • wait, that means I need to make two feed partials. one for groups and one for posts Commented Feb 19, 2013 at 2:01
  • 1
    no. i think you misundertood. inside post_feed_item, you don't have access to a feed_item, instead you should use post_feed_item. I think you can pass an as option to change the local variable name. render :partial => 'post_feed_item', :collection => feed_items, :as => :feed_item Commented Feb 19, 2013 at 2:06
  • aHA! That was the issue. I want to blame rails conventions for this, but clearly I should have found that somewhere. Thanks much. Do you want to make a brief answer to this question so I can mark it? Commented Feb 19, 2013 at 2:13

2 Answers 2

1

calling

= render partial: 'post_feed_item', collection: feed_items

will give you a post_feed_item local variable and not a feed_item variable. either you use that or pass in an as option to set the local variable name.

= render partial: 'post_feed_item', collection: feed_items, as: :feed_item
Sign up to request clarification or add additional context in comments.

4 Comments

How do you pass feed_item as an instance variable?
if you already have @feed_item, you can just access it directly in your partial or pass it as a local variable locals: { feed_item: @feed_tiem }
Sorry my question didn't include the whole question's scale. I have a partial which expects an instance variable and an array with instances which I want to pass to this partial. Example: collection: @items, as: :item where :item should be ‘:@item`. (Last part doesn't work.)
post another question in SO :)
0

I think you need to render it as a partial:

 %div.row
    %div.span6
      %h1 Posts 
      /Post feed
      =render :partial => 'feed',:locals => {:feed_items => @posts}
    %div.span6
      %h1 Groups
      /Group feed
      =render :partial => 'feed', :locals => {:feed_items => @groups}

6 Comments

let me give it a shot really quick
yes, sorry, using your recommendation brings me back to the original issue, which I'd obfuscated with more errors in my quest for a solution. The original issue is that the 2nd layer partial doesn't accept the collection item being passed to it. Check out the edit
What's the value of local_assigns[:feed_items] in your feed partial?
Yea if you put a debugger statement in your view or just write it out in the HTML.
gives me two different statements like this: #<ActiveRecord::Relation:0x007fa8e3b61e10>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.