2016-12-31 57 views
5

編輯:我設法得到它開始通過改變模型這引發錯誤:Rails的紙夾上傳不工作...沒有拋出錯誤,回滾事務

class User < ApplicationRecord 
    has_secure_password 

    has_attached_file :avatar, 
     :path => ":rails_root/public/system/:attachment/:id/:basename_:style.:extension", 
     :url => "/system/:attachment/:id/:basename_:style.:extension", 
     :styles => { 
      :thumb => ['100x100#', :jpg, :quality => 70], 
      :preview => ['480x480#', :jpg, :quality => 70], 
      :large => ['600>',  :jpg, :quality => 70], 
      :retina => ['1200>',  :jpg, :quality => 30] 
     }, 
     :convert_options => { 
      :thumb => '-set colorspace sRGB -strip', 
      :preview => '-set colorspace sRGB -strip', 
      :large => '-set colorspace sRGB -strip', 
      :retina => '-set colorspace sRGB -strip -sharpen 0x0.5' 
     } 

    validates_attachment :avatar, 
     :presence => true, 
     :size => { :in => 0..10.megabytes }, 
     :content_type => { :content_type => /^image\/(jpeg|png|gif|tiff)$/ } 

    belongs_to :leitung 
    def admin? 
     self.role == "Admin" 
    end 
    def al? 
     self.role == "Al" 
    end 
end 

控制檯現在看起來像這樣的:

Started POST "/leiter/new" for 127.0.0.1 at 2017-01-01 17:11:59 +0000 
Processing by Leiter::UsersController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"2z3H8rWo2TLADJtx3kWiCufulyLSBzLLCrxLGaLFH9BWOlGtMZ7BuPFCte8cEY5fC6yFVVggbi6vhESWLFFlRA==", "user"=>{"first_name"=>"asdf", "last_name"=>"asdf", "pfadi_name"=>"asdf", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "leitung_id"=>"1", "role"=>"asdf", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007feb301d8058 @tempfile=#<Tempfile:/tmp/RackMultipart20170101-6509-1yifkdk.jpg>, @original_filename="nobody_m.original.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"nobody_m.original.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create an account"} 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] 
Command :: file -b --mime '/tmp/3068f4f020f8c43d300b1e06b58b829b20170101-6509-tn2f1u.jpg' 
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3068f4f020f8c43d300b1e06b58b829b20170101-6509-q2rz7h.jpg[0]' 2>/dev/null 
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError> 
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3068f4f020f8c43d300b1e06b58b829b20170101-6509-q2rz7h.jpg[0]' 2>/dev/null 
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError> 
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3068f4f020f8c43d300b1e06b58b829b20170101-6509-q2rz7h.jpg[0]' 2>/dev/null 
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError> 
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/3068f4f020f8c43d300b1e06b58b829b20170101-6509-q2rz7h.jpg[0]' 2>/dev/null 
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError> 
    (0.1ms) begin transaction 
Command :: file -b --mime '/tmp/3068f4f020f8c43d300b1e06b58b829b20170101-6509-1p20xan.jpg' 
    Leitung Load (0.2ms) SELECT "leitungen".* FROM "leitungen" WHERE "leitungen"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 
    (0.0ms) rollback transaction 
Redirected to http://localhost:3000/leiter/new 
Completed 302 Found in 136ms (ActiveRecord: 0.4ms) 

然而,通常修復這個問題,development.rb文件設置並沒有工作:

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

(OP開始:)我正在設置回形針(使用ImageMagick),以便我的應用程序上的用戶可以上傳他們的帳戶的頭像。頭像應該保存在本地服務器上(說我的電腦,因爲應用程序目前正在開發中)。我按照GitHub上的文檔來設置回形針。一切似乎都在努力......直到我嘗試實際上傳圖像。提交後(在這種情況下,編輯現有用戶添加圖像,但同樣的事情發生在創建新用戶時),它不會拋出錯誤或任何東西,它只是重新加載頁面並灰色提交按鈕。控制檯中出現以下消息:

Started PUT "/leiter/edit" for 127.0.0.1 at 2016-12-31 18:33:19 +0000 
    ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" 
Processing by Leiter::UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"RjjDgMQESgZ/9DVXTWoTvJDitAmYXqfMyVJJyKpXDsp3hE0dejJ6erMlV+wsFEH9bFlz7ZsvBlgPO63o8PkpYw==", "user"=>{"first_name"=>"Admin", "last_name"=>"Admin", "pfadi_name"=>"Admin", "email"=>"[email protected]", "leitung_id"=>"6", "role"=>"Admin", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00000003671238 @tempfile=#<Tempfile:/tmp/RackMultipart20161231-3455-mekx7p.jpg>, @original_filename="nobody_m.original.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"nobody_m.original.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Speichern"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 
    (0.1ms) begin transaction 
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/3068f4f020f8c43d300b1e06b58b829b20161231-3455-1vvcs8q.jpg' 
Command :: PATH=/usr/local/bin/:$PATH; file -b --mime '/tmp/3068f4f020f8c43d300b1e06b58b829b20161231-3455-1cldo2f.jpg' 
    Leitung Load (0.2ms) SELECT "leitungen".* FROM "leitungen" WHERE "leitungen"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]] 
    (0.1ms) rollback transaction 
No template found for Leiter::UsersController#update, rendering head :no_content 
Completed 204 No Content in 81ms (ActiveRecord: 1.4ms) 

在視圖中,圖像應該出現的任何位置,它表示丟失。有趣的是,如果我不上傳圖片,它可以讓我創建和更新用戶,即使我有參數中需要的圖片。 我完全不知道爲什麼會發生這種情況。如果有幫助,這裏有我的用戶模型,我的控制器等:

用戶模型:

class User < ApplicationRecord 
    has_secure_password 
    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" 
    validates_attachment_content_type :avatar, :content_type => ["avatar/jpg", "avatar/jpeg", "avatar/png", "avatar/gif"] 
    belongs_to :leitung 
    def admin? 
     self.role == "Admin" 
    end 
    def al? 
     self.role == "Al" 
    end 
end 

用戶控制器:

class Leiter::UsersController < ApplicationController 
    def new 
     @newuser = User.new 
    end 
    def edit 
     @user = User.find(session[:user_id]) 
    end 
    def update 
     @user = User.find(session[:user_id]) 
     if @user.update_attributes(user_params) then 
      redirect_to '/leiter' 
     end 
    end 
    def create 
     @user = User.new(user_params) 
     if @user.save 
     redirect_to '/leiter' 
     else 
     redirect_to '/' 
     end 
    end 

    private 

    def user_params 
     params.require(:user).permit(:first_name, :last_name, :pfadi_name, :email, :password, :password_confirmation, :leitung_id, :role, :avatar) 
    end 
end 

查看更新用戶:

<%= simple_form_for @user, url: {action: "edit"}, html: { method: :put } do |f| %> 
    <%= f.text_field :first_name, :placeholder => "First name" %><br> 
    <%= f.text_field :last_name, :placeholder => "Last name" %><br> 
    <%= f.text_field :pfadi_name, :placeholder => "Pfadi name" %><br> 
    <%= f.email_field :email, :placeholder => "Email" %><br> 
    <%= f.association :leitung, :placeholder => "Leitung" %><br> 
    <%= f.text_field :role, :placeholder => "Rolle" %><br> 
    <%= f.input :avatar, as: :file %> 
    <%= f.submit "Speichern", class: "btn-submit" %> 
<% end %> 

如果您需要更多信息,我可以提供。感謝您的幫助,我確信我犯了一些小錯誤,但我找不到它......我是新手,但迄今爲止它非常有趣!

編輯:

gem 'rails', '~> 5.0.0', '>= 5.0.0.1' 
gem "paperclip", "~> 5.0.0" 
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux] 
+0

您使用的是Windows嗎? – boholdyjeramae

+0

@jeramaedybohol Nope,Ubuntu。 –

回答

-1

出於某種原因,我能以最奇特的方式來解決這個問題。我未安裝ImageMagick,Paperclip的依賴項,並突然一切工作?我絕對不能解釋爲什麼,但嘿,它的工作... 顯然不是最好的答案,爲什麼發生這種情況,但至少我現在可以繼續前進。我希望這個問題在轉向生產時不會再出現。

+0

這是誤導,你可以有兩個ImageMagick的安裝。 –