Can I insert a string into my model after save?

So I am following the directions on the RoR wiki for uploading images
(the new 2.0.2 directions) :
http://wiki.rubyonrails.org/rails/pages/HowtoUploadFiles

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

Any help would be appreciated!

Thanks for the reply!

Yes update triggers an after_save because I can overwrite the image by
editing it. I will try this, thank you very much.

yaphi wrote:

  ...
  ...
  set cover to "#{id}.#{extension}"
end

end

You mean this?

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.

To update, use update_attribute like this:

self.update_attribute :cover, “#{id}.#{extension}”

However, I don’t know if that will infinitely recurse, because you are
inside an
after_save event. Does update trigger another after_save event?

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?

yaphi wrote:

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.

<%= form_tag @photo, :html => { :multipart => true } do |f| %>

I think.

Hey Alex,

Uploading the file is not my problem. The problem is adding the:

self.update_attribute :logo, “#{id}.#{extension}”

to the write_file method. I want the file to upload (which it does),
then I want it to insert the filename into the logo column of the
table.

On May 28, 1:28 pm, Alex W. [email protected]

yaphi wrote:

Hey Alex,

Uploading the file is not my problem. The problem is adding the:

self.update_attribute :logo, “#{id}.#{extension}”

to the write_file method. I want the file to upload (which it does),
then I want it to insert the filename into the logo column of the
table.

On May 28, 1:28�pm, Alex W. [email protected]

Well, if the exception is:

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.