# Share controller method when rendering partial

**URL:** <https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303>\
**Category:** Rails\
**Created:** [November 2, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303 "2009-11-02T14:29:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 2, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303/1 "2009-11-02T14:29:22Z")

</div>

Hi,  
I have a question regarding rendering partials:

I have this controller called twitter

class TwitterController \< ApplicationController

def index  
@twitter\_search = Twitter::Search.new(’#rails’)  
end  
end

In the twitter index views i render a partial:

\<%= render :partial =\> “shared/twitterlist” %\>

This work’s fine for the Twitter index view, but when I call the  
render partial from another view like dashboard/index like this:

\<%= render :partial =\> “shared/twitterlist” %\>

I get this error:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

What am I doing wrong here?  
Is it even possible to share data across the views like that?

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 2, 2009, 3:51pm UTC](https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303/2 "2009-11-02T15:51:24Z")

</div>

andkjaer wrote:

> Hi,  
> I have a question regarding rendering partials:
> 
> I have this controller called twitter
> 
> class TwitterController \< ApplicationController
> 
> def index  
> @twitter\_search = Twitter::Search.new(’#rails’)  
> end  
> end
> 
> In the twitter index views i render a partial:
> 
> \<%= render :partial =\> “shared/twitterlist” %\>

First of all you’re relying on implicit instance variables in your  
partials. You really shouldn’t do that. The current version of Rails  
automatically injects instance variable assigned in the controller into  
partials. I believe, however, that this behavior has been deprecated and  
may be removed in future versions of Rails. Instead you should  
explicitly pass local data into the partial with :locals =\>  
@twitter\_search, or similar technique.

> This work’s fine for the Twitter index view, but when I call the  
> render partial from another view like dashboard/index like this:
> 
> \<%= render :partial =\> “shared/twitterlist” %\>
> 
> I get this error:
> 
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.each

If you need that data available to pass into the shared partial then you  
need to assign that data in the controller calling used for rendering  
the view/partial.

> What am I doing wrong here?  
> Is it even possible to share data across the views like that?

Keep in mind that instance variables set in controllers are only  
available for one request/response cycle. Any data you need for a view  
you should assign in the controller action used to render the view.

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 2, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303/3 "2009-11-02T15:54:41Z")

</div>

Robert W. wrote:

> Keep in mind that instance variables set in controllers are only  
> available for one request/response cycle. Any data you need for a view  
> you should assign in the controller action used to render the view.

Also remember that before\_filter can be used in cases where you need to  
load data for multiple actions within a controller. This will help keep  
your code DRY.

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 2, 2009, 4:33pm UTC](https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303/4 "2009-11-02T16:33:28Z")

</div>

Thanks Robert,  
But one question.  
Where do I put the code called by an :before\_filter  
In the application\_controller ?

On 2 Nov., 15:54, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 3, 2009, 1:16am UTC](https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303/5 "2009-11-03T01:16:42Z")

</div>

If I’m not mistaken, you have TwitterController and a  
DashboardController.  
You could have a before\_filter (to set @twitter\_search) in each of these  
for  
the methods that result in the partial being rendered. I suppose you  
could  
also have a before\_filter in the ApplicationController, but not sure  
that’s  
the best idea.

Euwyn

---

<div class="post-metadata">

**Author:** ![andkjaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@andkjaer](https://www.ruby-forum.com/u/andkjaer)\
**Post date:** [November 3, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/share-controller-method-when-rendering-partial/178303/6 "2009-11-03T12:17:23Z")

</div>

But where shhould the method live? In the Application controller?

def twitter\_search  
…  
end
