The files upload beautifully, but I don’t know how to access the
images since it’s not inserting the file name into “cover” when the
file posts. It leaves it null. How can I say:
def write_file
if @file_data
…
…
…
set cover to “#{id}.#{extension}”
end
end
def write_file
if @file_data
File.makedirs("#{ALBUM_COVER_STORAGE_PATH}/#{id}")
File.open("#{ALBUM_COVER_STORAGE_PATH}/#{id}/#{id}.#{extension}",
“w”) {
|file| file.write(@file_data.read) }
# put calls to other logic here - resizing, conversion etc.
end
end
In that example, your current object is the model. You could write
self, for
example, and get your model’s attributes.
I am receiving this error
undefined method `original_filename’ for “10.png”:String
when trying this:
def write_file
if @file_data
File.makedirs("#{LOGO_PATH}/#{id}")
File.open("#{LOGO_PATH}/#{id}/#{id}.#{extension}", “w”) { |file|
file.write(@file_data.read) }
# put calls to other logic here - resizing, conversion etc.
self.update_attribute :logo, “#{id}.#{extension}”
end
end
def extension @file_data.original_filename.split(".").last
end
I am receiving this error
undefined method `original_filename’ for “10.png”:String
when trying this:
def write_file
if @file_data
File.makedirs("#{LOGO_PATH}/#{id}")
File.open("#{LOGO_PATH}/#{id}/#{id}.#{extension}", “w”) { |file|
file.write(@file_data.read) }
# put calls to other logic here - resizing, conversion etc.
self.update_attribute :logo, “#{id}.#{extension}”
end
end
def extension @file_data.original_filename.split(".").last
end
Any ideas why?
@file_data is the object that is complaining. You think its an uploaded
file object, but according to that error, its a “10.png”:String. This
is a common problem with your form. If you leave off the :html => {
:multipart => true } in your form tag, then your browser will only send
the filename, not the actual data.
undefined method `original_filename’ for “10.png”:String
And the only place your calling the method original_filename is here:
def extension @file_data.original_filename.split(“.”).last
end
Then @file_data is the string “10.png”, not a file object. Look
wherever you are assigning that instance variable. It won’t have an
original_filename method unless its actually an uploaded file.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.