EDIT: I managed to get it to start throwing errors by changing the model to this:
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
Now the console looks like this:
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)
However, the usual fix for this problem by setting the development.rb file for this does not work:
Paperclip.options[:command_path] = "/usr/local/bin/"
(Start OP :) I'm working on setting up paperclip (with ImageMagick) so that users in my application can upload avatars for their account. Avatars must be saved locally on the server (they say on my PC, because the application is under development only at the moment). I followed the GitHub documentation to create a clip. Everything seems to work ... Until I try to upload the image. After I submit (in this case, editing an existing user to add an image, but the same thing happens when creating a new user), it does not throw an error or anything else, it just reloads the page and gives a submit button. The following message appears in the console:
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)
In the views where the image should be displayed, the missing is indicated instead. Interestingly, it allows me to create and update users if I do not upload an image, even if I have the image required in the parameters. I absolutely do not know why this is happening. If this helps, here is my user model, my controllers, etc .:
User Model:
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
User controller:
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
View to update user:
<%= 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 %>
If you need more information, I can provide it. Thanks for the help, I'm sure I made some trivial mistake, but I canβt find it ... I am new to rails, but so far it has been a lot of fun!
EDIT:
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]