4.增加欄位到model
rails g migration add_image_to_img imgurl(需要用到上傳的那個model)
5. DB Migrate
rake db:migrate
6.修改model
mount_uploader :imgurl, ImgUploade
7.加入form
<%= f.file_field :imgurl %>
8.完成即可顯示圖片:
image_tag(@foo.imgurl_url) 或 image_tag(@foo.imgurl_url(:thumb)) #取得壓縮的圖檔,搭配補充3
補充設定:
1.縮圖設定:
vi app/uploaders/image_uploader.rb
2.將include CarrierWave::MiniMagick前面的註解拿掉(本例是使用MiniMagick,使用前需先下載)
於app/uploaders/img_uploader.rb修改: # Include RMagick or MiniMagick support: # Include CarrierWave::RMagick include CarrierWave::MiniMagick
3.修改縮圖大小:
於app/uploaders/img_uploader.rb加入: version :thumb do process :resize_to_fit => [50, 50] end version :rectangle do process :resize_to_fit => [300, 300] end
4.只允許上傳圖片
於app/uploaders/img_uploader.rb加入: def extension_white_list %w(jpg jpeg gif png) end
5.修改儲存路徑
於app/uploaders/img_uploader.rb加入: def store_dir 'public/my/upload/directory' end
Pages: 1 2