# Ajax: update partial without the layout?

**URL:** <https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023>\
**Category:** Rails\
**Created:** [December 12, 2005, 6:53am UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023 "2005-12-12T06:53:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [December 12, 2005, 6:53am UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023/1 "2005-12-12T06:53:19Z")

</div>

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?

🙂

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [December 12, 2005, 6:57am UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023/2 "2005-12-12T06:57:46Z")

</div>

Try for ajax requests

xhr?

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [December 12, 2005, 7:58am UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023/3 "2005-12-12T07:58:47Z")

</div>

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? 🙂

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [December 12, 2005, 8:12am UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023/4 "2005-12-12T08:12:51Z")

</div>

On Dec 11, 2005, at 10:58 PM, Sean S. 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 Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [December 12, 2005, 8:42am UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023/5 "2005-12-12T08:42:59Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [December 12, 2005, 2:41pm UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023/6 "2005-12-12T14:41:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [December 13, 2005, 8:31am UTC](https://www.ruby-forum.com/t/ajax-update-partial-without-the-layout/50023/7 "2005-12-13T08:31:18Z")

</div>

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 🙂
