【问题标题】:Uploading an image with paperclip? Rails 4用回形针上传图片?导轨 4
【发布时间】:2014-10-16 18:57:42
【问题描述】:

问题: 没有任何东西被添加到数据库表中。请参阅下面的代码和错误。有人可以解释我做错了什么吗?谢谢!


User模特:

  has_attached_file :avatar, styles: { thumb: "48x48>" }, default_url: "/assets/:style/missing.png"
  validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }

Dashboard控制器:

  def update
    @user = User.find(current_user)
    @user.update( user_params )
    redirect_to dashboard_path
  end

  def user_params
    params.require(:user).permit(:avatar)
  end

路线:

  patch 'profile', to: 'user_dashboard#update'

表单/视图:

<%= form_for @user, url: profile_path, :html => { :multipart => true } do |f| %>
  <%= f.file_field :avatar %>
  <%= f.submit %>
<% end %>

错误:

Started PATCH "/profile" for 127.0.0.1 at 2014-08-22 16:31:30 -0400
Processing by UserDashboardController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"TV/CIi/DeO6r8yG5LbqQWWzAl619D7G6QB4fgyoTBRQ=", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x0000010b530ed8 @tempfile=#<Tempfile:/var/folders/74/_w090lhj6550xfrvb0rqyyq00000gn/T/RackMultipart20140822-23296-12aqgxz>, @original_filename="apply-icon.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"apply-icon.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Update User"}
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
   (0.2ms)  BEGIN
Command :: file -b --mime '/var/folders/74/_w090lhj6550xfrvb0rqyyq00000gn/T/9eb0b264a0bb1300e51e27b15fd7013c20140822-23296-6bphp2.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/74/_w090lhj6550xfrvb0rqyyq00000gn/T/9eb0b264a0bb1300e51e27b15fd7013c20140822-23296-195tr2e.png[0]' 2>/dev/null
sh: line 1: 23444 Trace/BPT trap: 5       identify -format '%wx%h,%[exif:orientation]' '/var/folders/74/_w090lhj6550xfrvb0rqyyq00000gn/T/9eb0b264a0bb1300e51e27b15fd7013c20140822-23296-195tr2e.png[0]' 2> /dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: file -b --mime '/var/folders/74/_w090lhj6550xfrvb0rqyyq00000gn/T/9eb0b264a0bb1300e51e27b15fd7013c20140822-23296-1ywe70.png'
   (0.4ms)  ROLLBACK

更新

移除图片缩放的作品:

用户模型:

  has_attached_file :avatar, default_url: "/assets/:style/missing.png"
  validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }

【问题讨论】:

  • 我相信validation 也应该是has_attached_filevalidates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
  • 有什么区别?我明确定义了 content_types.../\Aimage\/.*\Z/ – 做了什么?
  • 您好像没有安装 ImageMagick?
  • 不,我做了..我设法让它工作了..在@Rich下面看到我的答案,谢谢! ;)

标签: ruby-on-rails ruby-on-rails-4 paperclip


【解决方案1】:

发现这个错误有一个github问题here

解决方案应该是把application.rb中的以下内容设置为imagemagick的安装路径。

Paperclip.options[:command_path] = "/usr/local/bin/"

还可以尝试将样式包装在 {}

has_attached_file :avatar, { styles: { thumb: "48x48&gt;" }}

【讨论】:

  • 您是否使用自制软件安装了 imagemagick?如果是这样,您的 imagemagick 安装路径可能是 /usr/local/bin。此选项告诉回形针在开发中安装 imagemagick 的位置。打开 application.rb 文件或 development.rb 文件并将该行添加到该文件并重新启动 rails 服务器。
【解决方案2】:

挠了一阵头后..我通过执行以下操作找到了解决方案:

brew unlink libtool && brew link libtool
 #=>Unlinking /usr/local/Cellar/libtool/2.4.2... 0 symlinks removed
 #=>Linking /usr/local/Cellar/libtool/2.4.2... 17 symlinks created

并将Paperclip.options[:command_path] = "/usr/local/bin/" 添加到Application.rb

最后更新的用户#model:

  has_attached_file :avatar, styles: {thumb: "100x100"}, default_url: "/assets/:style/missing.png"
  validates_attachment :avatar, :presence => true,
    :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png"] },
    :size => { :in => 0..500.kilobytes }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2020-05-17
    • 2017-08-26
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多