我正在使用paperclip
gem来操纵图片并将其放在我的Gemfile
中。我还安装了ImageMagick
并且我发出命令which convert
或which identify
,我得到了/opt/ImageMagick/bin/
。
所以在development.rb
我添加了
Paperclip.options[:command_path] = "/opt/ImageMagick/bin/"
我的控制器代码用于处理post -
的创建def create
@post = Post.create(post_params)
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:image, :caption)
end
Post
的模型
class Post < ActiveRecord::Base
validates :image, presence: true
has_attached_file :image, styles: { :medium => "640x" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
虽然当我从视图页面点击提交时,我收到错误 -
Paperclip::Error in PostsController#create
There was an error processing the thumbnail for
终端输出 -
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eBXNVsIDE9vuiHgesSPm+mcWiNMz9s0Vgot27HKZl8uK+UUF5P6YnL47SW65Z0taX1GtGPYzceN+HvDseVA/Ow==", "post"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007f810f984508 @tempfile=#<Tempfile:/var/folders/nq/mk1lf5tj5_z4n9tyg0wtkmy80000gq/T/RackMultipart20150823-28735-1t6e63j.jpg>, @original_filename="IndiaEpic.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"IndiaEpic.jpg\"\r\nContent-Type: image/jpeg\r\n">, "caption"=>"india"}, "commit"=>"Create Post"}
Command :: PATH=/opt/ImageMagick/bin/:$PATH; file -b --mime '/var/folders/nq/mk1lf5tj5_z4n9tyg0wtkmy80000gq/T/2e0348f0f5226c94029d825fc44da2cd20150823-28735-1vy891l.jpg'
Command :: PATH=/opt/ImageMagick/bin/:$PATH; identify -format '%wx%h,%[exif:orientation]' '/var/folders/nq/mk1lf5tj5_z4n9tyg0wtkmy80000gq/T/2e0348f0f5226c94029d825fc44da2cd20150823-28735-1exazk5.jpg[0]' 2>/dev/null
Command :: PATH=/opt/ImageMagick/bin/:$PATH; identify -format %m '/var/folders/nq/mk1lf5tj5_z4n9tyg0wtkmy80000gq/T/2e0348f0f5226c94029d825fc44da2cd20150823-28735-1exazk5.jpg[0]'
Command :: PATH=/opt/ImageMagick/bin/:$PATH; convert '/var/folders/nq/mk1lf5tj5_z4n9tyg0wtkmy80000gq/T/2e0348f0f5226c94029d825fc44da2cd20150823-28735-1exazk5.jpg[0]' -auto-orient -resize "640x640" '/var/folders/nq/mk1lf5tj5_z4n9tyg0wtkmy80000gq/T/18ed538b6a2c563c650b284627c4609d20150823-28735-modfab'
非常感谢您解决此错误的任何帮助。如果我需要添加更多细节,请告诉我。
答案 0 :(得分:1)
你在Post
模型中犯了一个错误。 has_attached_file
是正确的,但styles:
正在抛出异常,因为您没有为图片指定正确的尺寸
您有以下内容:
has_attached_file :image, styles: { :medium => "640x" } # forgot to add your width in the string.
尝试更改为:
has_attached_file :image, styles: { :medium => "640x640" }