hello:
I have code for upload image file to server file system.This works
in rails
1.2.3.But in rails 2.1,it gets an error.
The following is the important code:
In a view:
<%form_for
:user,:url=>{:action=>"register"},:html=>{:multipart=>true}
do |f|-%>
<p>
<label>照片</label>
<%=f.file_field "file"-%>
</p>
<%end-%>
#############################
In a model:
class User < ActiveRecord::Base
validates_format_of :photo,:with=>%r{\.(jpg|png|gif)$}i,
:message=>"图片格式不正确"
def file=(file_field)
File.open("#{RAILS_ROOT}/public/images/users/#{file_field.original_filename}","wb+")
{ |i| i.write(file_field.read)}
self.photo="/images/users/"+file_field.original_filename
end
end
##############################
In a controller:
class LoginController < ApplicationController
def register
@user=User.new(params[:user])
if params[:user]&&@user.save
flash[:register]="#{@user.name}已创建"
redirect_to :action=>"upanel"
else
render :action=>"register"
end
end
end
#######################
when i submit the form,i get the following error:
undefined method `original_filename'' for "":String
############
how do i make the upload code work?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---