Inserting partial from different controller problem

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

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

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.

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.

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.

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs