1.rails new nana
2.cd nana
=>rails g scaffold post name:string content:text
3.在 Gemfile 里面添加了
gem 'carrierwave'
gem 'rmagick'
gem 'mime-types'
====>bundle install
4.rails g uploader image
5.修改 image_uplodaer.rb
class AttachmentUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
include CarrierWave::MimeTypes
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :set_content_type
version :thumb, :if => :image? do
process :resize_to_fit => [50, 50]
end
.....................
protected
def image?(new_file)
new_file.content_type.include? 'image'
end
end
==============>抱歉暂时不会用区块
6.rails g model attach image:string content_type:string
7.attach.rb ==>
mount_uploader :image, ImageUploader
belongs_to :post
8.post.rb==>
has_many :attach
9.views/posts/_form.html.erb==>
<%= f.file.field :attach%>
10.修改下其他的东西,去创建时,报错
ActiveModel::MassAssignmentSecurity::Error in PostsController#create
Can't mass-assign protected attributes: attach
不解,我都声明了 belongs_to&&has_many 怎么还是传不了啊