# How to use a controller/model in a different view

**URL:** <https://www.ruby-forum.com/t/how-to-use-a-controller-model-in-a-different-view/132313>\
**Category:** Rails\
**Created:** [March 19, 2008, 3:54am UTC](https://www.ruby-forum.com/t/how-to-use-a-controller-model-in-a-different-view/132313 "2008-03-19T03:54:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tresero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@tresero](https://www.ruby-forum.com/u/tresero)\
**Post date:** [March 19, 2008, 3:54am UTC](https://www.ruby-forum.com/t/how-to-use-a-controller-model-in-a-different-view/132313/1 "2008-03-19T03:54:02Z")

</div>

I am trying to put a random rss feed in the main (application) view.  
The code works if I surf to /feed/, but if I use the same partial  
outside of views/feeds/ I get errors about nil objects. It seems that  
the partial does not automatically use it’s controller.

I hope this makes sense.

---

<div class="post-metadata">

**Author:** ![tresero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@tresero](https://www.ruby-forum.com/u/tresero)\
**Post date:** [March 19, 2008, 9:49am UTC](https://www.ruby-forum.com/t/how-to-use-a-controller-model-in-a-different-view/132313/2 "2008-03-19T09:49:14Z")

</div>

On 19 Mar 2008, at 02:53, tresero wrote:

> I am trying to put a random rss feed in the main (application) view.  
> The code works if I surf to /feed/, but if I use the same partial  
> outside of views/feeds/ I get errors about nil objects. It seems that  
> the partial does not automatically use it’s controller.

Partials and views don’t belong to a controller in particular (except  
in the sense that their location on disk implies the controller that  
normally uses them). Your partial probably depends on the controller  
having setup some instance variable which you are setting up in your  
FeedController, but not elsewhere.

Fred

---

<div class="post-metadata">

**Author:** ![tresero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@tresero](https://www.ruby-forum.com/u/tresero)\
**Post date:** [March 19, 2008, 11:09am UTC](https://www.ruby-forum.com/t/how-to-use-a-controller-model-in-a-different-view/132313/3 "2008-03-19T11:09:15Z")

</div>

> The code works if I surf to /feed/, but if I use the same partial  
> outside of views/feeds/ I get errors about nil objects. It seems that

just pass it the parameter it needs, no? (this will create a local  
variable, though, just to be a little more precise) -

render :partial =\> ‘feeds/partialname’, :locals =\> { :var =\> @var }

---

<div class="post-metadata">

**Author:** ![tresero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@tresero](https://www.ruby-forum.com/u/tresero)\
**Post date:** [March 20, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/how-to-use-a-controller-model-in-a-different-view/132313/4 "2008-03-20T22:27:51Z")

</div>

Hi,

On Thu, Mar 20, 2008 at 1:16 PM, tresero [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= render :partial =\> ‘feeds/rss’, :locals =\> { :feed =\> @feed } %\>
> 
> Still getting a nil error:  
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.url
> 
> So it is never initializing feed.

It’s initializing “feed”, not “@feed”. Change your partial to use a  
local variable instead, change other places that render the partial to  
provide feed as a local variable, and you should be good.

~ j.

---

<div class="post-metadata">

**Author:** ![tresero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bb73d2/32.png) [@tresero](https://www.ruby-forum.com/u/tresero)\
**Post date:** [March 20, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/how-to-use-a-controller-model-in-a-different-view/132313/5 "2008-03-20T21:17:16Z")

</div>

Unfortunately that does not work. It appears that calling a partial  
does not initialize.

I have:  
@feed = Feed.find\_by\_handle(‘handle’)  
@feed.sync if @feed  
in my application controller.

in application view:

\<%= render :partial =\> ‘feeds/rss’, :locals =\> { :feed =\> @feed } %\>

Still getting a nil error:  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.url

Extracted source (around line #1):

Feed: \<%= @feed.handle -%\>  
  
Title: \<%= @feed.title -%\>  
  
Description: \<%= @feed.description -%\>

So it is never initializing feed.

On Mar 19, 2:09 am, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
