# Calling partials from onother controller/view

**URL:** <https://www.ruby-forum.com/t/calling-partials-from-onother-controller-view/176922>\
**Category:** Rails\
**Created:** [October 8, 2009, 11:09am UTC](https://www.ruby-forum.com/t/calling-partials-from-onother-controller-view/176922 "2009-10-08T11:09:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [October 8, 2009, 11:09am UTC](https://www.ruby-forum.com/t/calling-partials-from-onother-controller-view/176922/1 "2009-10-08T11:09:59Z")

</div>

Hi,

I’m learning rails and my first application is a simple gallery that  
pulls photos from flickr using flickraw.

The site has 3 main zones:

Left: where all albums are listed  
Center: where a photo is shown  
Bottom: where I have a few thumbnails

I already build everything but using only one Controller. Now I would  
like to separate some actions and have something like this:

Controllers

Gallery

- index (main page)

Albuns

- index (list all photosets)
- show (specific phototset from show/id)

Photos

- index (list all photos)
- show (specific photo from show/id)

controllers/albums\_controller.rb:

class AlbumsController \< ApplicationController  
require ‘flickraw’  
@@key = ‘123’  
@@user= ‘123’

def index  
@albums = flickr.photosets.getList(:api\_key =\> @@key, :user\_id =\>  
@@user)  
end  
end

views/albums/index.html.erb

\<% content\_for :albums do %\>  
\<%= render ‘shared/albums’ %\>  
\<% end %\>

views/shared/\_albums.html.erb

\<% albums.each do |a| %\>  
\<%= link\_to (a.title, :controller =\> ‘albums’, :action =\> ‘show’, :id  
=\> a.id) %\>  
  
\<% end %\>

The problem is I can’t share partials between views from other  
Controllers.  
If I call the partial from the controller Albums everything work as  
expected.  
If I call it from other Controller, the variable albums is nil.

I already used :object =\> @albums, :collections =\> @albums and locals =\>  
{ :albums =\> @albums } but nothing.

What is wrong?

Thanks

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [October 8, 2009, 11:20am UTC](https://www.ruby-forum.com/t/calling-partials-from-onother-controller-view/176922/2 "2009-10-08T11:20:54Z")

</div>

2009/10/8 Nuno N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Bottom: where I have a few thumbnails  
> Â - index (list all photosets)  
> Â @@key = ‘123’  
> \<% content\_for :albums do %\>  
> Â \<%= render ‘shared/albums’ %\>  
> \<% end %\>
> 
> views/shared/\_albums.html.erb
> 
> \<% albums.each do |a| %\>

I don’t know whether it is the problem or a typo in the post, but that  
should be @albums.each

Colin

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [October 8, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/calling-partials-from-onother-controller-view/176922/3 "2009-10-08T19:16:16Z")

</div>

To render the partial, try:

…  
\<%= render :partial=\>‘shared/albums’ %\>  
…

Jeff

On Oct 8, 2:09 am, Nuno N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [October 9, 2009, 9:58am UTC](https://www.ruby-forum.com/t/calling-partials-from-onother-controller-view/176922/4 "2009-10-09T09:58:29Z")

</div>

2009/10/8 Nuno N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > On Oct 8, 2:09ï¿½am, Nuno N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> No luck ☹

when you say ‘No luck’ do you mean you see nothing in the browser or  
there is some sort of error? Some things to try if you have not  
thought of them already:

If you see nothing in the browser have a look at the html (View, Page  
source or similar in browser) and see if there is anything there from  
the partial.

Edit the partial (if necessary) so that it will always display  
something (just some text maybe) even if the dynamic data that it  
should show is not getting through, check the html again.

Try removing the partial file and make sure that an error is seen,  
this will check that it is picking up the correct file.

Have a look in the log and see if anything useful there.

Use ruby-debug to break at various points to check the execution is  
going where you expect.

Colin

---

<div class="post-metadata">

**Author:** ![nfn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b19c9b/32.png) [@nfn](https://www.ruby-forum.com/u/nfn)\
**Post date:** [October 8, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/calling-partials-from-onother-controller-view/176922/5 "2009-10-08T23:54:52Z")

</div>

Jeff B.systems wrote:

> To render the partial, try:
> 
> …  
> \<%= render :partial=\>‘shared/albums’ %\>  
> …
> 
> Jeff
> 
> On Oct 8, 2:09ï¿½am, Nuno N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

No luck ☹

Any ideas?
