Hi,
I''m trying to create a file upload thingie. I''ll show you the
code first:
Controller
params[:photos].each do |k, v|
@photo = Photo.new do |p|
v.rewind
p.user_id = session[:user].id
p.file = v.read
logger.debug "filename = #{v.original_filename}"
logger.debug "title = = #{p.title}"
p.original_filename = v.original_filename
p.content_type = v.content_type
p.size = v.size
end
logger.debug "photo original name =
#{@photo.original_filename}"
logger.debug "photo ext = #{@photo.extension}"
logger.debug "photo title = #{@photo.title}"
if @photo.save
@valid << @photo
else
@invalid = true
end
... etc
Model
...
attr_accessor :file, :content_type, :size, :extension
attr_reader :original_filename
def original_filename=(filename)
@original_filename = filename
self.title = filename ##do later
ext_array = filename.split(''.'')
@extension = ext_array.pop
end
def validate_on_create
if @size <= 0
errors.add_to_base("File size for _#{@original_filename}_
(#{@size}) is invalid. #{@title} #{@size} #{@original_filename}
#{@file}")
else
if @content_type != "jpg"
errors.add_to_base("Invalid file type.")
end
end
end
...
-----------------
My question:
This is the validation error I receive after submitting a file:
Title can''t be blankFile size for __ (0) is invalid. 0
As you can see the original filename and @size are not accessible in
validate_on_create..
Only self.title is a db column. The others are strictly for my model.
I was told that i needed to use self.title for db column variables and
@variable in the other cases.
In my log file:
filename = Cibelle Photo 10.jpg
title = = Cibelle Photo 10.jpg
photo original name = Cibelle Photo 10.jpg
photo ext = jpg
photo title = Cibelle Photo 10.jpg
which is from these commands:
logger.debug "filename = #{v.original_filename}"
logger.debug "title = = #{p.title}"
logger.debug "photo original name =
#{@photo.original_filename}"
logger.debug "photo ext = #{@photo.extension}"
logger.debug "photo title = #{@photo.title}"
so here it does work.. what is wrong?
Thanks in advance