# Render :update and best practices

**URL:** <https://www.ruby-forum.com/t/render-update-and-best-practices/123087>\
**Category:** Rails\
**Created:** [December 13, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/render-update-and-best-practices/123087 "2007-12-13T22:39:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [December 13, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/render-update-and-best-practices/123087/1 "2007-12-13T22:39:07Z")

</div>

I’m pretty new to Rails and was wondering what the best practice is  
with respect to render :update. Should logic be placed in the :update  
blocks or should the logic be outside the blocks with separate blocks  
provided for each case?

i.e.

render :update do |page|  
if condition1  
page.replace\_html …  
\<rest of code for condition 2\>  
else  
page.replace\_html…  
\<rest of code for condition 2\>  
end

or

if condition1  
render :update do |page|  
\<code for condition 1\>  
end  
else  
render :update do |page|  
\<code for condition 2\>  
end  
end

Also, when is it preferable to use an RJS template over render :update?

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [December 18, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/render-update-and-best-practices/123087/2 "2007-12-18T17:15:04Z")

</div>

El jue, 13-12-2007 a las 13:38 -0800, bigbanger  
escribió:\> I’m pretty new to Rails and was wondering what the best practice is

> else  
> else  
> render :update do |page|  
> \<code for condition 2\>  
> end  
> end

I usually do it like second option (less code possible in render block),  
but not sure which is better and why.

–  
Jonathan Hernández Velasco aka jBilbo

> **[Jonathan Hernandez](https://jbilbo.com/)**
>
> I’ve not found any blog post or article about how to easily test CORS support implemented in a Rack middleware (e.g. using the rack-cors gem) …

President de GPLtarragona ;; [http://www.gpltarragona.org](http://www.gpltarragona.org)

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [December 18, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/render-update-and-best-practices/123087/3 "2007-12-18T19:21:17Z")

</div>

Frankly, you shouldn’t use render :update … ever. It is sort of like  
“render :text” in that it should be used sparingly to never.

The inherent problem with render :text, :inline, or :update is that it  
is clearly putting view code in the controller. View code (anything  
describing html, css, javascript or changes to them) should simply not  
be in the controller.

Look at this code

render :update do |page|  
page[‘list’].replace\_html “some new html”  
page[‘list’].replace\_html “some new html”  
page[‘list’].replace\_html “some new html”  
page[‘foo’].visual\_effect :highlight  
end

Clearly this type of code is all view-based and belongs in the view  
folder within an rjs template.

The answer is to do something like this:

if condition1  
render :action =\> “cond1.js.rjs”  
else  
render :action =\> “cond2.js.rjs”  
end

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [December 18, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/render-update-and-best-practices/123087/4 "2007-12-18T20:29:48Z")

</div>

As you have asked for better practice to use render :update. I would  
like to  
say best is that dont use render :update, have related rjs file for  
action.  
You could write render :update in controller action. But it is again  
MVC…  
render :update call is just for ui… we update divs, replace divs with  
the  
help of it. It gets difficult to handle also. There should be separation  
between view code and controller code.

– Anil

On Dec 14, 2007 3:08 AM, bigbanger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> page.replace_html ...
> <code for condition 1>
> 
> ```
> 
> end  
> else  
> render :update do |page|  
> \<code for condition 2\>  
> end  
> end
> 
> Also, when is it preferable to use an RJS template over render :update?
> 
> >

–  
Anil

> **[Anil Wadghule](https://anilwadghule.com)**
>
> Anil Wadghule - Elixir, Phoenix Software Developer
