# Inserting partial from different controller problem

**URL:** <https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672>\
**Category:** Rails\
**Created:** [May 5, 2008, 11:33am UTC](https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672 "2008-05-05T11:33:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 5, 2008, 11:33am UTC](https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672/1 "2008-05-05T11:33:25Z")

</div>

Hi,

I have a form to add links to youtube. for that form I hide an “Add”  
button if the user is not logged in. I’m using a partial to insert the  
form.

 \<%= render :partial =\> "layouts/video\_form" %\>

The contents of the partial are something like:

 \<% form\_for([@tab, @video]) do |f| %\> ...

```
<% if canComment() %>
  <%= f.submit "Add" %>
<% else %>
  <b>Login to add videos</b>
<% end %>

```

\<% end %\>

This works fine when I run it from the main controller (called tabs). In  
that controller the @tab and @video objects are known.

From that same page I can also log in, but the sessions are handled by a  
session controller, not the tab controller in which we are. So when a  
new session is created it instructs to render that partial again:

page.replace\_html ‘video’, :partial =\> ‘layouts/video\_form’

And my problem is that at that point the @tab object is nil:

ActionView::TemplateError (Called id for nil, which would mistakenly be  
4 – if you really wanted the id of nil, use object\_id) on line #2 of  
layouts/\_video\_form.html.erb:  
1:

  
2: \<% form\_for([@tab, @video]) do |f| %\>
* * *

How can I avoid losing the @tab object when the partial is instructed to  
be inserted from a different controller?

Thanks

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 5, 2008, 11:40am UTC](https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672/2 "2008-05-05T11:40:38Z")

</div>

On 5 May 2008, at 10:33, comopasta Gr wrote:

> * * *
> 
> How can I avoid losing the @tab object when the partial is  
> instructed to  
> be inserted from a different controller?

You’re thinking about this backwards. You need to ensure that @tab and  
@video are defined. (eg when the user logs in remember where they cam  
from so you know which video to render the partial for)

Fred

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 5, 2008, 11:40am UTC](https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672/3 "2008-05-05T11:40:39Z")

</div>

if i understand your setup right, then you can’t

you have most likely a submit or redirect there for the login, so the  
browser requests information from the server independently of any  
further requests.

i would advise to store this stuff in the session, but most likely you  
don’t have one before the login.

so the only thing you can do is to explicitly load the @tab & @video in  
your sessions controller.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 5, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672/4 "2008-05-05T12:31:12Z")

</div>

> > so the only thing you can do is to explicitly load the @tab & @video in  
> > your sessions controller.

Wait, yep I have a submit in the tabs page for the login process.  
So you mean that I could pass the objects there as parameters and then  
could load them again in the other controller so the partial can use  
them.

This is part of the login form:

\<%= submit\_to\_remote ‘commit’, ‘Log in’,  
:url =\> {:action =\> ‘create’, :controller =\> ‘session’},  
:after =\> “new Effect.SlideUp(‘login\_area’);” %\>

Regards.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 5, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672/5 "2008-05-05T12:25:24Z")

</div>

Hi, thanks for the comments.

Yeah there is no session before loggin in. So if I understood correctly  
one way to solve this is to store where the user was when the login  
process fires up. I actually had to do that to improve overall  
navigation but I was hoping to avoid it for this. I guess now is the  
time to get that in place.

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [May 5, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/inserting-partial-from-different-controller-problem/136672/6 "2008-05-05T23:01:29Z")

</div>

Hi,

just to close the issue here’s how I got it solved.

In the form where the user logs in I pass now the tab id:

\<%= submit\_to\_remote ‘commit’, ‘Log in’,  
:url =\> {:action =\> ‘create’, :controller =\> ‘session’, :thetab =\> @tab}  
%\>

In the sessions controller I load the tab and build a video for it:

@tab = Tab.find(params[:thetab])  
@video = @tab.videos.build

Now the partial rendering is able to use the right objects needed:

page.replace\_html ‘video’, :partial =\> ‘layouts/video\_form’

It seems to work great.

Thanks for the hints again!
