簡體   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