github 說明
https://github.com/thoughtbot/paperclip
先裝gem
gem 'paperclip', '~> 5.1'
$ bundle install
看說明的標題 Image Processor
ImageMagick must be installed
所以如果是mac這樣裝
$ brew install imagemagick
到標題 Quick Start
要做 Model, Migration, view, controller,改他的程式碼
先做 Model
# app/models/pic.rb 在你的 model 修改它的範例程式碼
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
end
其中 :avatar 是欄位名稱,我改成
class Pic < ActiveRecord::Base
belongs_to :user
has_attached_file :image, styles: { medium: "300x300>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
再做 Migration
$ rails g paperclip pic image
pic 是你的model,image 是其欄位名(github的說明用 avatar)
$ rake db:migrate
views 就如說明上顯示欄位;controller則是 permit 多一個欄位
new, edit 照標題Edit and New Views的程式碼使用,改成你的model, field
在index, show 照標題View Helpers的程式碼使用,改成你的model, field
就可以新增/修改/show照片了。
沒有留言:
張貼留言