回形针不保存附件

发布于 2024-12-20 07:04:59 字数 4366 浏览 4 评论 0原文

我是 Rails 和 Web 开发的新手...

我已经创建了一个用户模型,现在我正在尝试让用户能够使用 Paperclip 添加个人资料图片。

在我的用户显示页面中,用户可以单击链接打开“编辑”页面,从中可以看到要浏览的表单并选择要上传的图像。单击按钮时,它会调用“更新”操作并重定向到用户显示页面,但图像不会保存在任何文件夹中,并且图像属性(文件名、内容类型、文件大小)在数据库中仍设置为 NIL 。

  • 我已经安装并测试了 ImageMagick
  • 我添加了 :multipart =>; 表单中
  • true 在我已经放置 attr_accessible :avatar 的
  • 我已经设置了回形针选项来查找转换所在的 '/usr/bin/'
  • 我已经运行了迁移
  • 我已经设置了 :url 和 :path

- 在控制器,我的更新操作是:

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:

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

并且我将调试信息打印到浏览器中。单击“上传”后,我得到以下信息:

{"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,未创建应存储用户图像的系统目录,没有“回形针附件保存”,日志中也没有提及回形针...

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

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

它工作得很好! ...我还测试了新文件夹的创建,没有管理员权限,一切正常......我很绝望:-(

有人可以帮助我吗?

I am new in Rails and web development...

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

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.

  • 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

My model code is:

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

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">}}

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")´

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 :-(

Can anyone help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2024-12-27 07:04:59

三天后才发现这一点:由于我使用了密码保护(密码是 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文