我尝试上传图片,但收到此错误:
所以这是我的控制器:
class RecipesController < ApplicationController
def create
@recipe = Recipe.new(recipe_params)
@recipe.chef = Chef.find(1) #temporal to use Gravatar
if @recipe.save
flash[:success] = "Your recipe was created successfully!"
redirect_to recipes_path
else
render :new
end
end
private
def recipe_params
params.require(:recipe).permit(:title, :summary, :description, :picture)
end
end
我的模特
class Recipe < ActiveRecord::Base
belongs_to :chef
validates :title, presence: true
validates :summary, presence: true
validates :description, presence: true
mount_uploader :picture, PictureUploader
validate :picture_size
private
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5 MB")
end
end
end
gem&#39; carrierwave&#39;,&#39; 0.10.0&#39; gem&#39; mini_magick&#39;,&#39; 3.8.0&#39;
两个Gems都在我的GemFile中,我在Windows上安装了ImageMagick。
ImageUploader.rb
class PictureUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
process resize_to_limit: [600,600]
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
希望你们能帮助我!