I am new to Rails and web development ...
I created a User model, and now I'm trying to give the user the ability to add a profile picture using Paperclip.
On my user display page, the user can click the link to open the edit page from which he can see the form for viewing and select an image to upload. When the button is clicked, it calls the "update" action and is redirected to the userβs display page, but the image is not saved in any folder, and the image attributes (file name, content type, file size) are still set to the NIL database in the database.
- I installed and tested ImageMagick
- I added
:multipart => true
in the form - I put attr_accessible: avatar
- I set the paperclip options to search for '/ usr / bin /' where the converter is located
- I completed the migration
- I set: url and: path
- in the controller, my update action:
def update @user = User.find(params[:id]) @title = "Update profile picture" response_to do |format| if @user.update_attributes(params[:user]) format.html {redirect_to(@user, :notice => 'Profile picture loaded')} else format.html {render :action => "edit", :notice => 'Unable to load pic")} end end end
My model code:
class User < ActiveRecord::Base attr_accessor :password attr_accessible :name, :email, :number_of_positive_reco, :confidence_percent, :password, :password_confirmation, :avatar, :avatar_file_name, :avatar_content_file, :avatar_file_size has_attached_file :avatar , :styles => { :medium => "300x300>", :thumb => "100x100>"}, :url => "/images/profiles/:attachment/:id_:style.:extension", :path => ":rails_root/public/images/profiles/:attachment/:id_:style.:extension" # :default_url => "/images/Default_profile_picture.png" email_regex = /\A[\w+\-.] +@ [az\d\-.]+\.[az]+\z/i validates :name, :presence => true, :length => { :maximum => 20} validates :email, :presence => true, :format => { :with => email_regex}, :uniqueness => {:case_sensitive => false} validates :password, :presence => true, :confirmation => true, :length => { :within => 6..40 } validates :number_of_positive_reco, :numericality => {:only_integer => true, :greater_than_or_equal_to => 0} validates :confidence_percent, :numericality => { :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 1.0} before_save :encrypt_password # Return true if the user password matches the submitted password. def has_password?(submitted_password) encrypted_password == encrypt(submitted_password) end def self.authenticate(email, submitted_password) user = find_by_email(email) return nil if user.nil? return user if user.has_password?(submitted_password) end def self.authenticate_with_salt(id, cookie_salt) user = find_by_id(id) (user && user.salt == cookie_salt) ? user : nil end private def encrypt_password self.salt = make_salt if new_record? self.encrypted_password = encrypt(password) end def encrypt(string) secure_hash("#{salt}--#{string}") end def make_salt secure_hash("#{Time.now.utc}--#{password}") end def secure_hash(string) Digest::SHA2.hexdigest(string) end end
The form is in the edit.html.erb file:
<h1> Ajouter une photo au profil </h1> <%= form_for @user, :html => { :multipart => true} do |f| %> <div class="field"> <%= f.label :avatar, "Upload ta photo" %> <br /> <%= f.file_field :avatar %> </div> <div class="actions"> <%= f.submit "Upload" %> </div> <% end %>
And I printed the debug info in the browser. After clicking the "Download" button, I got the following:
{"commit"=>"Upload", "authenticity_token"=>"+ExcuQOSv1bxIyAoM5+N4TCSmYI8JYeh5Yb8P5W4VU0=", "_method"=>"put", "utf8"=>"β", "action"=>"update", "id"=>"8", "controller"=>"users", "user"=>{"avatar"=>
So, in the log I see that the "paperclip" fields are filled with the image name, image type, etc., but there is no "INSERT to TABLE", all user fields are still NIL, the system is the directory in which the user image should be stored , does not create a "Bond attachment in paper clips", nor the mention of a paper clip in a magazine ...
In console mode, I can create a new user by setting the avatar attributes as:
`User.create (: avatar => File.new (Rails.root +" public / images / an_image.png ") '
and everything works fine! ... I also tested creating a new folder without administrator privileges, and it works fine ... I'm distracted: - (
Can anybody help me?