Rails 回形针上传不工作...没有抛出错误,回滚事务

Rails Paper Clip upload not working... No error thrown, rolls back transaction

编辑:我设法通过将模型更改为以下内容来让它开始抛出错误:

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 %>

如果您需要更多信息,我可以提供。感谢您的帮助,我确定我犯了一些微不足道的错误,但我找不到它...我是 rails 的新手,但到目前为止非常有趣!

编辑:

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]

出于某种原因,我能够以最特殊的方式解决这个问题。我卸载了 ImageMagick,它是 Paperclip 的依赖项,突然间一切正常了??我绝对无法解释为什么,但是,嘿,它奏效了…… 显然这不是为什么会发生这种情况的最佳答案,但至少我现在可以继续前进了。希望上线后不要再出现这个问题。