【问题标题】:Paperclip error on submit提交时回形针错误
【发布时间】:2015-08-23 12:31:19
【问题描述】:

我正在使用paperclip gem 来处理图像并将其保存在我的Gemfile 中。我也安装了ImageMagick,是我给命令which convert还是which identify,我得到/opt/ImageMagick/bin/回来。

所以在我的development.rb 中我添加了

Paperclip.options[:command_path] = "/opt/ImageMagick/bin/"

我的控制器代码来处理帖子的创建 -

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'

不胜感激帮助解决此错误。让我知道是否需要添加更多详细信息。

【问题讨论】:

  • 您能否发布您的模型代码文件以帮助进一步解决此问题?

标签: ruby-on-rails-4 paperclip


【解决方案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" }

【讨论】:

  • 您可以将 ImageMagick 导出到 $PATH 并修改您的 development.rb 并注释掉 Paperclip 选项。 source /opt/ImageMagick/bin/ 并在执行此操作时重新启动终端和 Rails 服务器。
  • 我的echo $PATH 输出 - /Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/aniruddhabarapatre1/.rvm/gems/ruby-2.2.1/bin:/Users/aniruddhabarapatre1/.rvm/gems/ruby-2.2.1@global/bin:/Users/aniruddhabarapatre1/.rvm/rubies/ruby-2.2.1/bin:/usr/local/bin:/Users/aniruddhabarapatre1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/opt/ImageMagick/bin:/usr/local/MacGPG2/bin:/Users/aniruddhabarapatre1/.rvm/bin
  • 如果你回显 ~/.bashrc、~/.bash_history 或 ~/.bash_profile 会发生什么?
  • /Users/aniruddhabarapatre1/.bashrc.bash_history, .bash_profile 相应地。
  • 通过进入 vim 编辑器查看.bashrc 并确保其中有 ImageMagick。提示:sudo vim ~/.bashrc
猜你喜欢
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 2012-07-10
  • 2014-06-04
相关资源
最近更新 更多