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