# Stupid Rails rendering

**URL:** <https://www.ruby-forum.com/t/stupid-rails-rendering/108324>\
**Category:** Rails\
**Created:** [July 2, 2007, 10:14am UTC](https://www.ruby-forum.com/t/stupid-rails-rendering/108324 "2007-07-02T10:14:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 2, 2007, 10:14am UTC](https://www.ruby-forum.com/t/stupid-rails-rendering/108324/1 "2007-07-02T10:14:34Z")

</div>

I dont’ understand why it’s not possible to have a conditional  
redirection in rendering

```
respond_to do |format|
format.js {
   render :update do |page|
     page.redirect_to posts_url if @located
     page.replace_html 'error_message', "Error..."
     page << "$('popup_error').popup.show();"
   end
 }
end

```

raise an error… =\> Can only render or redirect once per action  
what shoudl do if I don"t want to render OR redirect

```
respond_to do |format|
format.js {
   page.redirect_to posts_url if @located
   render :update do |page|
     page.replace_html 'error_message', "Error..."
     page << "$('popup_error').popup.show();"
   end
 }
end

```

tfyl

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 2, 2007, 10:36am UTC](https://www.ruby-forum.com/t/stupid-rails-rendering/108324/2 "2007-07-02T10:36:25Z")

</div>

There is no stupid errors here… when you open a render block, you are  
rendering a template at this moment… you can’t do other render or  
redirect inside… you can have a conditional redirection, but not  
inside a render, its pure logic.

Try doing it:

respond\_to do |format|  
format.js {  
if @located  
page.redirect\_to posts\_url  
else  
render :update do |page|  
page.replace\_html ‘error\_message’, “Error…”  
page \<\< “$(‘popup\_error’).popup.show();”  
end  
end  
}  
end

It’s similar, but not the same.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 4, 2007, 8:51am UTC](https://www.ruby-forum.com/t/stupid-rails-rendering/108324/3 "2007-07-04T08:51:24Z")

</div>

Kad

You have probably already spotted it, but just in case:

respond\_to do |format|  
format.js {  
if @located  
page.redirect\_to posts\_url  
else  
render :update do |page|  
page.replace\_html ‘error\_message’, “Error…”  
page \<\< “$(‘popup\_error’).popup.show();”  
end  
end  
}  
end

In the line - page.redirect, page is out of scope.

Will work if you do:

render :update do |page|  
if @located  
page.redirect\_to posts\_url  
else  
page.replace\_html ‘error\_message’, “Error…”  
page \<\< “$(‘popup\_error’).popup.show();”  
end  
end

The reason it needs to be this way is that redirect does not cause the  
method to immediately exit, so the other render  
statements were getting executed, causing the error.

tonypm

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [July 4, 2007, 2:18pm UTC](https://www.ruby-forum.com/t/stupid-rails-rendering/108324/4 "2007-07-04T14:18:33Z")

</div>

tonypm wrote:

> Kad
> 
> You have probably already spotted it, but just in case:

> The reason it needs to be this way is that redirect does not cause the  
> method to immediately exit, so the other render  
> statements were getting executed, causing the error.
> 
> tonypm

thanks a lot , yes I spotted it after rewritting the bloc line by  
line…  
I re-checked all other blocks I had with some begin - rescue -  
redirect\_to
