[英]Paperclip won't save image in Rails app
I am trying to use Paperclip with my Rails app to add an avatar to a user but it won't save my image or update the database column when creating the user. 我正在尝试使用Paperclip和我的Rails应用程序向用户添加头像,但在创建用户时它不会保存我的图像或更新数据库列。
This is what the model looks like: 这就是模型的样子:
class User < ActiveRecord::Base
has_attached_file :avatar
And the registerform in haml: 和haml中的registerform:
- form_for :user, @user, :url => { :action => "signup" }, :html => { :multipart => true } do |f|
...
...
%li
%div{:class => "header"} Profilepicture
%div{:class => "input"}
= f.file_field :avatar
And when I look in the log this is what is being passed to the "signup" action: 当我查看日志时,这是传递给“注册”操作的内容:
Parameters: {"commit"=>"Save", "action"=>"signup", "controller"=>"user/register", "user"=>{"name"=>"Micke Lisinge", "birthmonth"=>"07", "password_confirmation"=>"[FILTERED]", "nickname"=>"lisinge", "avatar"=>#<File:/tmp/RackMultipart20100426-3076-1x04oxy-0>, "gen"=>"m", "birthday"=>"23", "password"=>"[FILTERED]", "birthyear"=>"1992", "email"=>"lisinge@gmail.coma"}}
[paperclip] Saving attachments.
Paperclip says it is saving the template but when I look in the public
folder in my app it has created a system
but the system
folder is empty. Paperclip说它正在保存模板,但是当我查看我的应用程序中的
public
文件夹时,它创建了一个system
但system
文件夹是空的。
So it seems like it isn't saving the picture to the folder. 所以似乎没有将图片保存到文件夹中。
It gets handled by the form and saved in my /tmp
folder. 它由表单处理并保存在我的
/tmp
文件夹中。
Maybe you guys have any tips or know what this problem might be? 也许你们有任何提示或知道这个问题可能是什么?
I got it to work. 我得到了它的工作。
I had to add :avatar
to attr_accessible
in my user model . 我不得不在我的用户模型中添加
:avatar
attr_accessible
到attr_accessible
。
Posting this here and hopes that it helps someone sometime :) 在这里发布,并希望它有时帮助某人:)
Thanks guys for your help 谢谢大家帮助
Don't forget to set :multipart => true in your form declaration. 不要忘记在表单声明中设置:multipart => true。 This has bitten me once or twice.
这咬了我一两次。
has_attached_file :asset, :url => "/assets/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/:id/:style/:basename.:extension"
FYI, This code actually saved my files in the root directory of my machine "/" as the :rails_root param was failing. 仅供参考,这段代码实际上将我的文件保存在我的机器“/”的根目录中,因为:rails_root param失败了。 This is on Rails 3.0.0.rc
这是在Rails 3.0.0.rc上
First check, if path
is correct for the created attachment. 如果
path
对于创建的附件是正确的,请首先检查。 You can use avatar.path
to determine that. 您可以使用
avatar.path
来确定。 if it is not returning correct path, may be someone is overriding the default paperclip
path? 如果它没有返回正确的路径,可能有人会覆盖默认的
paperclip
路径?
Also check, if public/system is writable by the user as which you are running the application server. 如果公共/系统可由运行应用程序服务器的用户写入,请检查。
Try setting the :path option 尝试设置:path选项
has_attached_file :avatar,
:path => ':rails_root/public/system/:id.:extension'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.