Rails Paper Clip上传不起作用...没有抛出错误,回滚事务

时间:2016-12-31 18:46:47

标签: ruby-on-rails paperclip

编辑:我设法通过将模型更改为:

来让它开始抛出错误
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"=>"asdf@asdf.asdf", "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"=>"admin@application.com", "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]

1 个答案:

答案 0 :(得分:-1)

出于某种原因,我能够以最特殊的方式解决这个问题。我卸载了ImageMagick,Paperclip的依赖,突然一切都工作了?我绝对无法解释原因,但嘿,它有效... 显然不是最好的答案,为什么会这样,但至少我现在可以继续前进。我希望这个问题在转向生产时不再出现。