刘华佼的全栈学习记录

5-11 thumb.url是什么东东

| Comments

答案在这里CarrierWave

$ brew install imagemagick
class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  process resize_to_fit: [800, 800]

  version :thumb do
    process resize_to_fill: [200,200]
  end

end

When this uploader is used, an uploaded image would be scaled to be no larger than 800 by 800 pixels. A version called thumb is then created, which is scaled and cropped to exactly 200 by 200 pixels. The uploader could be used like this:

uploader = AvatarUploader.new
uploader.store!(my_file)                              # size: 1024x768

uploader.url # => '/url/to/my_file.png'               # size: 800x600
uploader.thumb.url # => '/url/to/thumb_my_file.png'   # size: 200x200

最后一句:
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
说明了.thumb.url是指用大小为200*200的图片。

教材5-5 Step 4 : 显示购物车明细的用法是:

             <% if cart_item.product.image.present? %>
              <%= image_tag(cart_item.product.image.thumb.url, class: "thumbnail") %>
            <% else %>
              <%= image_tag("http://placehold.it/200x200&text=No Pic", class: "thumbnail") %>
            <% end %>

教材4-4 STORY 3 - 上传图片的用法是:
app/views/admin/products/edit.html.erb

+ <% if @product.image.present? %>
+   <span>目前商品图</span> <br>
+   <%= image_tag(@product.image.thumb.url) %>
+ <% end %>

以及app/views/admin/products/index.html.erb:

    <% if product.image.present? %>
      <%= image_tag(product.image.thumb.url, class: "thumbnail") %>
    <% else %>
      <%= image_tag("http://placehold.it/200x200&text=No Pic", class: "thumbnail") %>
    <% end %>

这两个图片对应的url地址是:http://localhost:3001/uploads/product/image/1/thumb_goldfish-30837_960_720.png

这里面image是一个uploader.

Comments

comments powered by Disqus