在Dropbox上托管资源时检查是否存在连接

时间:2014-01-04 04:10:50

标签: ruby-on-rails ruby dropbox carrierwave dropbox-api

我借助宝石CarrierwaveCarrierwave-Dropbox在Dropbox上存储和阅读图片。

我不太确定内部工作,Carrierwave-Dropbox如何请求Dropbox API。我希望能够检查是否存在连接,因为我现在正在显示这样的图像:

<%= @user.profile_picture %>

并且在提取图片失败时(例如在localhost上离线),我得到SocketError

更新

我无法确定控制器或视图中是否已发生错误,但它似乎在视图中。这是服务器(在控制台中)的消息:

ActionView::Template::Error (getaddrinfo: nodename nor servname provided, or not known):

在浏览器中,错误页面的页面标题为:

"Action Controller: Exception caught" 

,浏览器窗口中的标题为红色:

SocketError in Admin::Users#show

1 个答案:

答案 0 :(得分:1)

不是访问视图中的profile_picture属性(从而冒着SocketError异常的风险),而是尝试将处理异常的逻辑移到控制器上:

# app/controllers/users_controller.rb
def show
  @user = User.find(params[:id])
  begin
    @profile_picture = @user.profile_picture
  rescue SocketError => e      
    flash[:error] = "#{e}"
    @profile_picture = nil
  end
end

然后,在您的视图中,您将可以访问@user对象以及@profile_picture(如果它存在;否则,它只是nil):< / p>

# app/views/users/show.html.erb
<%= @profile_picture %>
相关问题