# A question on redirect\_to

**URL:** <https://www.ruby-forum.com/t/a-question-on-redirect-to/141494>\
**Category:** Rails\
**Created:** [June 26, 2008, 1:24pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494 "2008-06-26T13:24:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 1:24pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/1 "2008-06-26T13:24:27Z")

</div>

Hello,

I have been trying to do something like this… I have an object which  
is being sent through to the view from the controller and since the  
object is not guarenteed to have non nil values, i use an exception  
handler to catch the exception upto which this works fine. However in  
the handler, I need to redirect to say an error page, it says  
“undefined method redirect\_to”.

The snippet from my view :

\<%begin %\>  
…  
…  
…

\<% end %\>  
\<%rescue =\> msg %\>  
\<% logger.info "Exception occurred - " + msg.to\_s %\>  
\<%= redirect\_to :controller =\> ‘my\_controller’, :action =\> ‘error’ %\>  
\<%end %\>

This view is rendered thorough another action from the same controller  
‘my\_controller’. When an exception occurs, it is caught and the point  
upto which the exception is rendered correctly. However, I would want  
the page to be redirected rather than being partially rendered. Is  
there anything that I am doing incorrectly here ? Any suggestions on  
this would be really helpful.

~Titan.

---

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/2 "2008-06-26T13:30:54Z")

</div>

Seems to me as if you’re having much too much logic  
in that view. Such decisions should be made in the controller  
The view should only display results.

---

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/3 "2008-06-26T13:32:39Z")

</div>

On 26 Jun 2008, at 12:24, Titan wrote:

> This view is rendered thorough another action from the same controller  
> ‘my\_controller’. When an exception occurs, it is caught and the point  
> upto which the exception is rendered correctly. However, I would want  
> the page to be redirected rather than being partially rendered. Is  
> there anything that I am doing incorrectly here ? Any suggestions on  
> this would be really helpful.

you can’t redirect from the view. You might be able to use a  
rescue\_from handler in your controller, although I think the approach  
you’re taking is a bit fugly.

Fred

---

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/4 "2008-06-26T13:45:32Z")

</div>

The issue you are having here is that you are trying to save yourself  
specific checks by using a global begin rescue block.

so if you dont have guranteed values. (I.e: columns which are not  
checked for presence) and want to display them in the view

then you should do something like:

\<%= @model.columnname.uppercase if @model.columname %\>

to avoid the error. “Undefined method uppercase for nil object”

yes, every time.

ps: you have your begin rescue bblock wrong.

begin  
code\_that\_might\_raise  
rescue Exception =\> e  
more\_code  
end

AND NOT

begin  
code\_that\_might\_raise  
end \<-------------- this is  
wrong.  
rescue Exception =\> e  
more\_code  
end

Hope it heps

---

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/5 "2008-06-26T14:02:32Z")

</div>

Thanks for all your quick thoughts on this. What Wolas had predicted  
is exactly the case that I am facing(a large set of legacy data); for  
which I wanted to have a global rescue blocks. But I had earlier faced  
some problems with rescue\_from while dealing with the views and so I  
guess I would have to have individual validations for the fields  
before rendering them.

thanks!  
~Titan.

---

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/6 "2008-06-26T13:56:05Z")

</div>

Does a rescue\_from in the controller rescue exceptions in the view? I  
had  
problems with this a while back and just assumed it didn’t.

RSL

On Thu, Jun 26, 2008 at 7:31 AM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/7 "2008-06-26T15:12:27Z")

</div>

On 26 Jun 2008, at 12:54, Russell N. wrote:

> Does a rescue\_from in the controller rescue exceptions in the view?  
> I had problems with this a while back and just assumed it didn’t.

can’t remember, which is why I said might :-). It seems that any  
exception thrown while rendering is wrapped inside an  
ActionView::TemplateError and so that’s the type you have to  
rescue\_from (which seems to work in a quick experiment)

Fred

---

<div class="post-metadata">

**Author:** ![Titan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ba0ec/32.png) [@Titan](https://www.ruby-forum.com/u/Titan)\
**Post date:** [June 26, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/a-question-on-redirect-to/141494/8 "2008-06-26T15:22:26Z")

</div>

aha! i’m sure that was it, i just wasn’t rescuing the right error.

RSL

On Thu, Jun 26, 2008 at 9:11 AM, Frederick C. \<
