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 %>
...
....<some code that might give exception>
...
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.
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.
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.
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.
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)