Search code examples
ruby-on-railstwittertwitter-gem

Do a rescue_from on Twitter::Error and still load view


I want to catch a Twitter::Error that happens in a view partial and still load the page. Put another way, I don't want a Twitter::Error to stop the page from loading. I just want that partial to not be loaded.

https://gist.github.com/scottmagdalein/903cd168ea4171365d51


Solution

  • You can rescue from errors to allow what you need:

    def my_method
      begin
        # do some things
      rescue Twitter::Error
        flash[:notice] = 'oh dear!'
      end
    
      # any other view code
    end
    

    You catch the error, set a message, then continue to your view.