This is for a rails 4.0.2 application running ruby 2.0.0-p353.
I have a helper that has the following method
def render_feed_row(activity_item)
begin
if activity_item.type == "comment"
render "newsfeed/comment", :item => activity_item
elsif activity_item.type == "post"
render "newsfeed/post", :item => activity_item
else
raise NameError
end
rescue NameError => e # note: NoMethodError is a subclass of NameError
render "newsfeed/other", :item => activity_item
end
end
But if a NoMethodError is raised in the newsfeed/post partial, it is not caught in this helper. Why is that? It don't render newsfeed/other, it actually raises an exception.
I've just checked with a simplified version of your code, and it worked correctly.
However, please note it's a very bad practice to use exceptions for control flow in such way. Moreover, if you really want to use exceptions, you should create your own classes, not use
NoMethodErrororNameErrorthat have specific meanings in Ruby programs.Here's a more clean version of your method
Or a more concise version