简体   繁体   English

Ruby on Rails回形针-文件未保存

[英]Ruby on Rails Paperclip - Files not saving

I have it setup so that my user model has the attached file :photo : 我进行了设置,以便我的用户模型具有附件文件:photo

has_attached_file :photo, :styles => { :small => "150x150>" },
              :url  => "../avatars/:basename.:extension",
              :path => ":rails_root/public/avatars/:basename.:extension"
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']

To try and make use of this, I've added a field for avatar uploads in the user edit view: 为了尝试利用此功能,我在用户edit视图中添加了一个用于头像上传的字段:

<% form_for @user, :html => { :multipart => true } do |f| %>
#Unimportant Stuff here
<%= f.file_field :photo %>
#Unimportant Stuff here
<% end %>

And then created the edit and update methods in my user controller: 然后在我的用户控制器中创建editupdate方法:

def edit
    @user = User.find(params[:id])
end

def update
    @user = User.find(params[:id])
    respond_to do |format|
     if @user.update_attributes(params[:user])
    format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
    format.xml  { head :ok }
     else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
     end
    end
end

My issue is that with all of this, uploading files still doesn't work. 我的问题是,所有这些仍然无法上传文件。 On visiting a users' page and uploading an image, I just get "Missing" where the image is supposed to be. 在访问用户页面并上传图像时,我只是得到了图像应该在的“丢失”位置。 If I set the image to a required field, I get an error stating that the filename is required (so I guess the uploading just isn't working correctly?). 如果我将图像设置为必填字段,则会收到错误消息,指出文件名是必需的(因此我猜上传只是无法正常工作?)。

Any help would be greatly appreciated. 任何帮助将不胜感激。

不要忘记创建应该将附件保存到其中的文件夹结构。

I'm not sure if your rails root can be setup like that. 我不确定您的rails根是否可以这样设置。 I have been using rails 2.3 for a project and mine is setup as #{RAILS_ROOT}/public/pdfs/:id/:style/:basename.:extension 我一直在将Rails 2.3用于一个项目,并将我的设置为#{RAILS_ROOT} /public/pdfs/:id/:style/:basename.:extension

This previous SO Post seems to agree with me. 之前的 SO Post似乎同意我的观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM