0

我有很多图片,偶尔有一两张无法正常访问。我不想插入一个空白点,而是想捕获错误而不添加任何内容。

错误:

Rendered /Users/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.6ms)

如何在 html.erb 中进行测试?

      <ul>
        <% @photos.each do |photo| %>
          <% if [HOW DO I TEST IT HERE?] %>  //if no error
            <li style="float: left;">
              <%= link_to image_tag(photo.image.url(:thumb)), photo %>
            </li>
          <% else %> //if it errors that it can't access the file
             // put nothing
          <% end %>
        <% end %>
      </ul>

另外,为什么它被称为/位于rescues/layout?我认为错误是由 处理的logger,还是这不同?

完整跟踪:

`Started GET "/system/images/13/thumb/f838fe117a5a1f03ff5d2056d026d61a.jpg?1358062373" for 127.0.0.1 at 2013-01-14 00:19:19 -0500

ActionController::RoutingError (No route matches [GET] "/system/images/13/thumb/f838fe117a5a1f03ff5d2056d026d61a.jpg"):
  actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.3) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.3) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.3) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.8) lib/rails/engine.rb:479:in `call'
  railties (3.2.8) lib/rails/application.rb:223:in `call'
  rack (1.4.3) lib/rack/content_length.rb:14:in `call'
  railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.3) lib/rack/handler/webrick.rb:59:in `service'
  /Users/me/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/me/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/me/.rvm/rubies/ruby-1.9.3-p362/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /Users/me/.rvm/gems/ruby-1.9.3-p362/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.6ms)`
4

1 回答 1

0

将您的逻辑放在辅助方法中

#PhotosHelper
def validate_image(img_path)
    "<li><img src=\"#{img_path}\"></li>" if File.exist?(img_path)
end

#view
<% @photos.each do |photo| %>
<%= validate_image(photo.image.url(:thumb))%>
<% end %>

您可以使用content_tag清理 validate_image 方法

于 2013-01-14T08:00:28.060 回答