A question on redirect_to

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

<% 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.

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.

Fred

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

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

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.

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

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

RSL