Forum: Ruby on Rails Ajax: update partial without the layout?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
450046d5f684ae5001c0b1a079d447d4?d=identicon&s=25 Sean Schertell (df-sean)
on 2005-12-12 06:53
How the heck do you do it?  I can't put this in the controller:
render(:layout => false)

...or it will never show the layout even the first time before the Ajax
call.  So I guess you have to use some sort of conditioal, but what's
the condition?

Here's the pseudo code I want to add to my controller:

def show_list
  if :is_ajax
    render(:layout => false)
  end
 # do stuff
end


Any tips for a newbie?

:-)
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-12-12 06:57
(Received via mailing list)
Try for ajax requests

xhr?
450046d5f684ae5001c0b1a079d447d4?d=identicon&s=25 Sean Schertell (df-sean)
on 2005-12-12 07:58
Daniel ----- wrote:
> Try for ajax requests
>
> xhr?


Is that a symbol?  Would my conditional look like this?
if :xhr?
  #blah blah
end

I tried that and it seems to always return true :(

So I'm still scratching my head.  Am I missing something really stupid
here?  Surely I'm not the first person to ever not want a second layout
when a partial is redered with ajax, right?

How do you guys do this?  :)
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-12 08:12
(Received via mailing list)
On Dec 11, 2005, at 10:58 PM, Sean Schertell wrote:

>
> I tried that and it seems to always return true :(
>
> So I'm still scratching my head.  Am I missing something really stupid
> here?  Surely I'm not the first person to ever not want a second
> layout
> when a partial is redered with ajax, right?
>
> How do you guys do this?  :)
>


if request.xhr?
    # do ajax stuff
else
    # do normal stuff
end

Cheers-

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
450046d5f684ae5001c0b1a079d447d4?d=identicon&s=25 Sean Schertell (df-sean)
on 2005-12-12 08:42
Ezra Zygmuntowicz wrote:
> if request.xhr?
>     # do ajax stuff
> else
>     # do normal stuff
> end

Ezra that's awesome thanks!  And it *almost* works too.  The conditional
does seem to be working.  But strangely enough, if I let it render with
the extra layout, there are no errors.  But if I use your conditional to
stop the second layout, I get this love note from Rails:

NoMethodError in Nova#members
Showing app/views/partial/_paginator.rhtml where line #4 raised:

You have a nil object when you didn't expect it!
The error occured while evaluating nil.current
Extracted source (around line #4):

1: <div class="paginator">
2:         <div class="button">
3:           <%=
4:           if paginator.current.previous
5:            link_to '<span class="hidden">&lt;&lt;</span>',
6:                     { :page => paginator.current.previous },
7:                       :class => 'back'


What the...?
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2005-12-12 14:41
(Received via mailing list)
Whats the actual code part for your rendering? I've just been using a
mix of partials and regular templates lately to update sections of the
page with out any real issues. If I'm rendering a partial my
controller simply has:
render :partial => "/partial/name/here"
Or if I have a non-partial I have
render :action => "action", :layout => false
Either way they both seem to work.  It is important to remember though
that just calling the render() method doesn't actually execute the
corresponding action method in the controller. You have to call them
separately in these cases.

Where I usually miss something is in the views them selves and I'll
spell the id for the div tag wrong or something. If you do that then
either nothing gets updated or other odd things happen.

So post some code, and we'll see whats happening.

-Nick
450046d5f684ae5001c0b1a079d447d4?d=identicon&s=25 Sean Schertell (df-sean)
on 2005-12-13 08:31
Thanks very much Nick.  In my case, the problem was resolved by simply
moving Ezra code to the bottom of my controller definition.  I can't
honestly claim to understand why that worked - but it did so all is
well.

Thanks everyone :)
This topic is locked and can not be replied to.