【问题标题】:Rails 3rd Party Services UnavaliableRails 3rd 方服务不可用
【发布时间】:2013-12-04 22:54:53
【问题描述】:

我在我的应用程序控制器中通过相关 gem 和以下方法将 Twitter 和 Instagram api 作为我的 Rails 应用程序之一,有没有办法将此代码包装在某些东西中,如果这些服务是允许网站仍然运行的不可用或当我在没有互联网连接的情况下在本地工作时。

应用控制器

helper_method :tweets, :instagram

def tweets
  Twitter.user_timeline("xxxxxxxx", :count => 3)
 end

def instagram
  Instagram.user_recent_media("xxxxxxxxx", :count => 6)
end

如果我关闭我的 wifi,那么这是我在 tweets 方法中遇到的错误

Twitter::Error::ClientError at /
getaddrinfo: nodename nor servname provided, or not known

【问题讨论】:

    标签: ruby-on-rails ruby twitter instagram


    【解决方案1】:

    试试这个,

    helper_method :tweets, :instagram
    
    def tweets
      Twitter.user_timeline("xxxxxxxx", :count => 3) rescue nil
     end
    
    def instagram
      Instagram.user_recent_media("xxxxxxxxx", :count => 6) rescue nil
    end
    

    【讨论】:

    • 在视图中添加 if 语句后效果很好。
    猜你喜欢
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2015-05-05
    相关资源
    最近更新 更多