Ajax: update partial without the layout?


#1

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?

:slight_smile:


#2

Try for ajax requests

xhr?


#3

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 :frowning:

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? :slight_smile:


#4

On Dec 11, 2005, at 10:58 PM, Sean S. wrote:

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

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? :slight_smile:

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

Cheers-

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732


#5

Ezra Z. 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:


2:

3: <%=
4: if paginator.current.previous
5: link_to ‘<<’,
6: { :page => paginator.current.previous },
7: :class => ‘back’

What the…?


#6

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


#7

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 :slight_smile: