[Rails] Uploaded File to Database

Dieter Komendera
Mon, 06 Mar 2006 20:00:41 -0000

Hi,

I currently write a tool for my school. Therefore I have to save
uploaded files.
I bought Agile Web Dev. with Rails to learn webdev with Rails.
I''m very new at rails and couln''d find anything on the
internet.

Now when I want to save the file data to the mysql database, rails gives
me an error.
My code is the same as written in the book on page 363.

Here is my code of the model:

class Document < ActiveRecord::Base
   has_many :comments
     def file=(file_field)
       self.filename = base_part_of(file_field.original_filename)
       self.content_type = file_field.content_type.chomp
       self.data = file_field.read
   end
     def base_part_of(file_name)
       name = File.basename(file_name)
       name.gsub(/[^\w._-]/, '''')
   end
end

The data column in the database is blob

the file seems to be uploaded correctly, but something goes wrong.
I get this error message:

ActiveRecord::StatementInvalid in Admin#create

Mysql::Error: You have an error in your SQL syntax near
''''3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d225554462d38222073746''
at line 1:

I assume the long number is the binary data of the file.
The part of question in the query string is this:

... ''filename.ext'', x''3c3f786d6c2076657273696f ...

When copy the whole query string from the error message into a text
editor, delete the x after the comma, paste the string then into
phpMyAdmin and execute it, everything goes well.

If I omit saving the data, everything goes well.

Addtional info:
Rails version: 1.0.0
MySQL version: 3.23.58

So what can I do, to force Rails to do not write the x into the query
string?
Or what else can I do?

Regards
Dieter Komendera


--
Posted via http://www.ruby-forum.com/.