回形针不保存附件 [英] Paperclip does not save attachment

查看:142
本文介绍了回形针不保存附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails和网站开发的新用户...

I am new in Rails and web development...

我创建了一个User模型,我现在试图给用户添加一个个人资料图片,使用回形针。

I have created a User model, and I am now trying to give the user the ability to add a profile picture, using Paperclip.

在我的用户显示页面上,用户可以点击链接打开编辑页面,他可以从中查看表单以选择图片上传。当点击按钮时,它调用更新动作并重定向到用户显示页面,但图像不保存在任何文件夹中,图像属性(filename,contenttype,filesize)在数据库中仍设置为NIL

From my user show page, a user can click on a link to open an 'edit' page, from which he can see a form to browse and choose an image to upload. When clicking on the button, it calls the 'update' action and redirect to the user show page, but the image is not saved in any folder, and the image attributes (filename, contenttype, filesize) are still set to NIL in the database.


  • 我安装并测试了ImageMagick

  • 我已经添加了 :multipart => true

  • 我已设置回形针选项以查找/
  • usr / bin /'转换位于
  • 我已运行迁移

  • 我设置了:url和:path

  • I have installed and tested ImageMagick
  • I have added the :multipart => true in the form
  • I have put attr_accessible :avatar
  • I have set the paperclip options to look for '/usr/bin/' where convert is located
  • I have run the migration
  • I have set :url and :path

- 在控制器中,更新操作为:

-in the controller, my update action is:

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

我的模型代码是:

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+\-.]+@[a-z\d\-.]+\.[a-z]+\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's 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

表单位于edit.html.erb中:

The form is located in edit.html.erb:

<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 information into the browser. After clicking Upload, I got this:

{"commit"=>"Upload", "authenticity_token"=>"+ExcuQOSv1bxIyAoM5+N4TCSmYI8JYeh5Yb8P5W4VU0=", "_method"=>"put", "utf8"=>"✓", "action"=>"update", "id"=>"8", "controller"=>"users", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0xb6d63fec @content_type="image/jpeg", @original_filename="Paperclip-Railway.jpg", @tempfile=#<File:/tmp/RackMultipart20111208-1681-3h3ps4-0>, @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"Paperclip-Railway.jpg\"\r\nContent-Type: image/jpeg\r\n">}}

所以,在日志中,我看到'回形针的字段'填充了图像名称,图像类型等等,但没有INSERT into TABLE,所有用户字段仍然是NIL,不创建用户的图像应该存储的系统目录,没有回形针附件保存,也没有提到在日志中的回形针...

So, in the log, I see that the 'paperclip's fields' are filled with image name, image type, etc...but there is no "INSERT into TABLE", all the user fields are still NIL, the system directory where user's image should be stored is not created, there is no "Paperclip attachment save", nor any mention of paperclip in the log...

在控制台模式下,我可以创建一个新用户,将头像属性设置为:

In console mode, I can create a new user, setting the avatar attributes as :

`User.create(:avatar => File.new Rails.root +public / images / an_image.png)'

`User.create(:avatar => File.new(Rails.root + "public/images/an_image.png")´

...我也测试了一个新文件夹的创建,没有管理员权限,它的工作所有罚款...我是desesperate: - (

and it works just fine! ... I also tested the creation of a new folder, without admin rights, and it works all fine ... I am desesperate :-(

任何人都可以帮助我?

推荐答案

3天内发现:由于我使用密码保护(密码是attr_accessor),所以不可能更新用户,而无需在表单中添加密码字段。

3 days to find this out: as I used password protection (password being attr_accessor), it's impossible to update a user, without adding the password field in the form.

尝试在不输入密码的情况下编辑个人资料照片不起作用,并且没有任何错误消息让我想起

Trying to edit the profile picture without entering the password does not work, and no error message that could have me made me think about this was generated.

因此,在编辑视图中,不要忘记在表单中添加密码字段,以便能够更新用户的图片!

So, in the edit view, don't forget to add the password field in the form to be able to update user's picture!

这篇关于回形针不保存附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆