我正在使用带有 CarrierWave gem 的 Rails 4.0.0。为什么不在数据库中的属性总是等于nil?它仅在文件 PostUploader 中。最终,数据到达。如何确保这些属性在文件 PostUploader 中可用?
class PostUploader < CarrierWave::Uploader::Base
...
version :thumb do
process :crop
end
def crop
model.image_crop # => nil
end
...
end
模型:
validates :name, presence: true
mount_uploader :image, PostUploader
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
控制器:
def create
@post = Post.new(post_params)
...
end
def post_params
params.require(:post).permit(:name, :image, :crop_x, :crop_y, :crop_w, :crop_h)
end
post_params
{"name"=>"trololo", "image"=>#, @original_filename="large (3).jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"large (3).jpg\"\r\nContent-Type: image/jpeg\r\n">, "crop_x"=>"0", "crop_y"=>"0", "crop_w"=>"100", "crop_h"=>"200"}
PostUploader 中的模型对象:
Post id: nil, name: "trololo", image: nil, created_at: nil, updated_at: nil
和
model.crop_x && model.crop_w always => nil