kenan
unread,Feb 8, 2012, 12:26:08 PM2/8/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to carrierwave
I'm trying to upload some images with extension jpg. They are student
pictures where all are named with student numbers.
eg: "11860012.jpg"
For the students who do not have photo, i use a default picture which
is named as "default.jpg". What i want is to upload these pictures to
different directories according to ile names.
eg: while uploading, if the file path is /path/to/file/default.jpg,
store_dir should be "uploads/#{model.class.to_s.underscore}/
#{mounted_as}/defult"
on the other hand if the file path is /path/to/file/11860012.jpg,
store_dir should be "uploads/#{model.class.to_s.underscore}/
#{mounted_as}/#{student_program_code}", where student_program_code is
a method specifies the program code for each student (there are 3
programs and over 5000 students).
Anyway, when i have store_dir method as;
<==================================================================
def store_dir
if original_filename == "default.jpg"
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/default/"
else
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/
#{student_program_code}"
end
end
def student_program_code
student_number = original_filename.split('.').first
year = "20" + original_filename[0,2]
student = Student.find_by_student_number(student_number)
program_code = student.programs.first.code.parameterize
result = [program_code, year].compact.join('_')
result
end
===================================================================>
and with the following rake task;
<===================================================================
desc 'upload student pictures'
task :upload_student_pictures, [:filepath, :program_id]
=> :environment do |t, args|
Student.transaction do
program = Program.find(args.program_id)
program.students.each do |student|
begin
file = File.open("#{args.filepath}/
#{student.student_number}.jpg")
rescue => e
file = File.open("#{args.filepath}/default.jpg")
puts e
ensure
student.picture = file
student.save!
end
end
end
end
=====================================================================>
it assigns the filenames under Student model picture field. But when i
call picture on any student, it results with error
ActionView::Template::Error (You have a nil object when you didn't
expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.split):
4:
5: <table class="list">
6: <tr>
7: <td class="headline"><%=
image_tag(@student.picture.url(:thumb).to_s) unless
@student.picture.url.nil? %></td>
8: <td class="context"></td>
9: </tr>
10: <tr>
app/uploaders/picture_uploader.rb:20:in `store_dir'
when i check original_filename on rails console, i get the
original_filename. But the error says "The error occurred while
evaluating nil.split"..
I could not figured out what is wrong with it..Any help?