I am trying to upload a user profile picture to my deviser user model. I kept getting a lot of errors mostly because of spoofing. I think I managed to fix that now by installing File for Windows and adding the path to development.rb.
I stopped getting errors, however the resulting images do not show up anywhere.
This is my form:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {method: :put, :multipart => true }) do |f| %>
<%= f.file_field :image %>
<div class="actions">
<%= f.submit "Update Avatar", class: 'btn btn-large btn-block btn-success' %>
</div>
<% end %>
This is registrations controller:
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :telephone, :image)
end
This is development.rb
Paperclip.options[:command_path] = "C:/Program Files (x86)/GnuWin32/bin"
This is my user model:
validates :first_name, presence: true
validates :last_name, presence: true
validates :telephone, presence: true
has_attached_file :image, {styles: { small: "64x64", med: "100x100", large: "200x200" },
:url => "/assets/images/users/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/images/users/:id/:style/:basename.:extension",
:default_url => "users/default/:style/missing.png"}
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validates_attachment_size :image, :less_than => 1.megabytes
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
This is my server log:
Started GET "/users/edit" for 127.0.0.1 at 2015-03-11 15:30:24 +0200
Processing by Users::RegistrationsController#edit as HTML
[1m[35mUser Load (1.0ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Rendered layouts/_error.html.erb (0.0ms)
Rendered users/registrations/edit.html.erb within layouts/application (21.0ms)
Completed 200 OK in 200ms (Views: 196.0ms | ActiveRecord: 1.0ms)
Started PUT "/users" for 127.0.0.1 at 2015-03-11 15:30:28 +0200
Processing by Users::RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qZNCmCZNuqdBcvOUcUwmlRbkFqpw87BmWc05ONHFQ9E=", "user"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x84062d0 @tempfile=#<Tempfile:C:/Users/User2/AppData/Local/Temp/RackMultipart20150311-1372-6v6ud8>, @original_filename="missing.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[image]\"; filename=\"missing.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Update Avatar"}
[1m[36mUser Load (1.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1[0m
[1m[35mUser Load (0.0ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
[1m[36m (0.0ms)[0m [1mBEGIN[0m
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & file -b --mime "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-1o5kdt1.png"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & identify -format '%wx%h,%[exif:orientation]' "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]" 2>NUL
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & identify -format %m "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & convert "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]" -auto-orient -resize "64x64" "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn20150311-1372-jdwt01"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & file -b --mime "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn20150311-1372-jdwt01"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & identify -format '%wx%h,%[exif:orientation]' "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]" 2>NUL
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & identify -format %m "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & convert "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]" -auto-orient -resize "100x100" "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn20150311-1372-1y940vr"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & file -b --mime "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn20150311-1372-1y940vr"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & identify -format '%wx%h,%[exif:orientation]' "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]" 2>NUL
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & identify -format %m "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & convert "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn.png[0]" -auto-orient -resize "200x200" "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn20150311-1372-k4dqyf"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & file -b --mime "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-yr7xvn20150311-1372-k4dqyf"
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & file -b --mime "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-mg3zzp.png"
[1m[35mSQL (0.0ms)[0m UPDATE `users` SET `image_content_type` = 'image/png', `image_file_name` = 'missing.png', `image_file_size` = 3792, `image_updated_at` = '2015-03-11 13:30:28', `updated_at` = '2015-03-11 13:30:29' WHERE `users`.`id` = 1
[1m[36m (78.0ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/
Completed 302 Found in 966ms (ActiveRecord: 79.0ms)
This is in my initializers:
module Paperclip
class MediaTypeSpoofDetector
private
def type_from_file_command
# -- original code removed --
# begin
# Paperclip.run("file", "-b --mime-type :file", :file => @file.path)
# rescue Cocaine::CommandLineError
# ""
# end
# -- new code follows --
begin
Paperclip.run("file", "-b --mime :file", :file => @file.path)
rescue Cocaine::CommandLineError
""
end
end
end
end
If I remove method: :put from my form I get this in my server log:
Command :: SET PATH=C:/Program Files (x86)/GnuWin32/bin;%PATH% & file -b --mime "C:/Users/User2/AppData/Local/Temp/ea21841da70e6405af19fabc4ff8bdd920150311-1372-1m4o85d.png"
[1m[36mSQL (1.0ms)[0m [1mUPDATE `users` SET `image_file_name` = 'missing.png', `image_updated_at` = '2015-03-11 13:46:37', `updated_at` = '2015-03-11 13:46:38' WHERE `users`.`id` = 1[0m
[paperclip] deleting C:/edumax/edumax/public/assets/images/users/1/original/missing.png
[paperclip] deleting C:/edumax/edumax/public/assets/images/users/1/small/missing.png
[paperclip] deleting C:/edumax/edumax/public/assets/images/users/1/med/missing.png
[paperclip] deleting C:/edumax/edumax/public/assets/images/users/1/large/missing.png
[1m[35m (65.0ms)[0m COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 1111ms (ActiveRecord: 67.0ms)
I don't quite understand why paperclip is deleting the images at end
Aucun commentaire:
Enregistrer un commentaire