I have it setup so that my user model has the attached file :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:
<% 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:
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, upload开发者_高级运维ing 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.
Don't forget to create the folder structure where the attachments are supposed to be saved into.
加载中,请稍侯......
精彩评论