# Render :update returning null

**URL:** <https://www.ruby-forum.com/t/render-update-returning-null/146949>\
**Category:** Rails\
**Created:** [August 27, 2008, 1:23am UTC](https://www.ruby-forum.com/t/render-update-returning-null/146949 "2008-08-27T01:23:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [August 27, 2008, 1:23am UTC](https://www.ruby-forum.com/t/render-update-returning-null/146949/1 "2008-08-27T01:23:52Z")

</div>

I wondered if anyone can help me, I am trying to add some basic ajax to  
my new project but I’ve run into a problem.

I have a RESTful ProductsController which uses pagination, I would like  
to basically re-render the index template on susbequent ajax calls to  
the index action. However the following doesn’t work

format.js do  
render (:update) do |page|  
page.replace\_html ‘main’, :template =\> ‘index’  
end  
end

The response I receive is

Element.update(‘main’,null);

which has the effect of empty the contents of my ‘main’ div, which is  
not very helpful. It seems the following works, but its not what I need.

page.replace\_html ‘main’, :partial =\> ‘somepartial’

Any one have any ideas, do I just have the syntax wrong?

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [August 27, 2008, 8:49am UTC](https://www.ruby-forum.com/t/render-update-returning-null/146949/2 "2008-08-27T08:49:45Z")

</div>

I thought replace\_html receives a :partial option, and not a :template  
option that is unknown… I think the nil object happens because there  
isn’t :partial element as argument.

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [August 27, 2008, 9:34am UTC](https://www.ruby-forum.com/t/render-update-returning-null/146949/3 "2008-08-27T09:34:17Z")

</div>

I think you’re right 🙂 Unforunately I need to re-render the whole  
template (minus the layout) not just a partial. Without separating the  
content of my template into a partial for the sake of making this work  
(which feels very wrong) I can’t see a way around it.

Any ideas?

Eduardo YÃ¡Ã±ez Parareda wrote:

> I thought replace\_html receives a :partial option, and not a :template  
> option that is unknown… I think the nil object happens because there  
> isn’t :partial element as argument.

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [August 27, 2008, 10:05am UTC](https://www.ruby-forum.com/t/render-update-returning-null/146949/4 "2008-08-27T10:05:57Z")

</div>

On 27 Aug 2008, at 08:34, Rob L. wrote:

> I think you’re right 🙂 Unforunately I need to re-render the whole  
> template (minus the layout) not just a partial. Without separating the  
> content of my template into a partial for the sake of making this work  
> (which feels very wrong) I can’t see a way around it.

This is quite disgusting but:  
in your controller @content = render\_to\_string :template =\> ‘foo’  
in your rjs file : page.replace\_html ‘main’, @content

However you don’t need to do this, eg

link\_to\_remote ‘click me’, :url =\> …, :update =\> ‘main’

Then in you action just do a bog standard render :template =\> …

Fred

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [August 28, 2008, 10:05am UTC](https://www.ruby-forum.com/t/render-update-returning-null/146949/5 "2008-08-28T10:05:42Z")

</div>

Indeed it is disgusting, but sometimes that’s necessary to solve a  
problem ☹  
However I’ve managed to solve it, typically it was easier than I thought

format.js do  
render :update do |page|  
page.replace\_html ‘main’, :file =\> ‘products/index’  
end  
end

Yay for the Rails source.

> This is quite disgusting but:  
> in your controller @content = render\_to\_string :template =\> ‘foo’  
> in your rjs file : page.replace\_html ‘main’, @content
> 
> However you don’t need to do this, eg
> 
> link\_to\_remote ‘click me’, :url =\> …, :update =\> ‘main’
> 
> Then in you action just do a bog standard render :template =\> …
> 
> Fred
