我不确定图片上传是否只是没有保存,或者我是否将它们保存在错误的位置或出了什么问题..现在当我使用以下代码生成图片标签时:
<%= image_tag @photo.image_url.to_s %>
它只是抛出一个路由错误:
No route matches "/images"
我想设置这条路线吗?..无论如何,我一直在关注 railscasts.org 上的 tut 这里有一些更相关的代码:
<%= form.file_field :image %> #in the form
mount_uploader :image, ImageUploader #in the model Photo
#in the image_uploader file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
#also nothing special going on in the controller
def create
@photo = Photo.new(params[:photo])
respond_to do |format|
if @photo.save
format.html { redirect_to(@photo, :notice => 'Photo was successfully created.') }
format.xml { render :xml => @photo, :status => :created, :location => @photo }
else
format.html { render :action => "new" }
format.xml { render :xml => @photo.errors, :status => :unprocessable_entity }
end
end
end