繁体   English   中英

用回形针上传图像? 滑轨4

[英]Uploading an image with paperclip? Rails 4

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


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"] }

有此错误的一个GitHub的问题发现在这里

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

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

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

has_attached_file :avatar, { styles: { thumb: "48x48>" }}

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

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

最近更新的User#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 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM