我有一個文件中的字段:驗證對於要上傳個人資料圖片的人的模型
<%= f.file_field :file %>
而這一切都工作好,不過,我不知道如何驗證它。
這裏是我創造的動作,這一切都完美的工作,但分裂params哈希表像我這樣做可能是錯誤的:
def create
new_user_params = user_params
image_params = user_params[:profile_image_attributes]
new_user_params.delete("profile_image_attributes")
@user = User.new(new_user_params)
respond_to do |format|
if @user.save
@user.create_thumbnail(image_params)
sign_in @user
format.html { redirect_to @user, notice: 'Welcome to ' + request.host_with_port + ', ' + @user.user_name + '!' }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
和這裏的user#create_thumbnail
方法:
def create_thumbnail(data)
upload = data['file'].read
img = Magick::Image.from_blob(upload).first
img.resize!(75,75)
transaction do
self.create_profile_image(path: 'test')
img.write 'test' + '.' + img.format.downcase
end
end
問題:
就像這樣說,它一切正常,,但我想知道如何正確地做到這一點,最重要的是,如何阻止人們上傳大小不超過格式.gif,.png .jpg或.jpeg的大文件和文件以及相應的驗證錯誤消息...
@ user3067865我的回答有助於解決您的問題嗎?讓我知道。 –