File_column Problems

Posted on April 20, 2006

OK, how do I make file_column work? I did the following (after installing file_column) which seems like it could be correct, however, it doesn’t work.

1.) Added a database migration


class AddOpmlFileColumn < ActiveRecord::Migration
  def self.up
    create_table :opmlfiles do |table|
      table.column :id, :integer, :null=>false
      table.column :file_name, :string, :null=>false
      table.column :created_at, :datetime
      table.column :user_id, :integer, :null=>false, :default=>'0'
    end
  end

def self.down
  drop_table :opmlfiles
end

end

2.) Created an Opmlfile model

class Opmlfile < ActiveRecord::Base
  file_column :opmlfile
end

3.) Updated the generated controller’s constructor

class OpmlfilesController < ApplicationController
  def new
    if request.get?
      @opmlfile = Opmlfile.new
    else
    	@opmlfile = Opmlfile.new(params[:opmlfile])
    	@opmlfile.save
    end
  end

4.) Designed an upload form

<%= start_form_tag :action => 'create', :multipart => true %>
<p>Upload file:
<%= file_field('opmlfile', 'file_name') %>
<%= submit_tag('Upload file') %>
<%= end_form_tag %>
<%= link_to 'Back', :action => 'list' %>
</p>

OK, the thing is, it looks like it is uploading during the HTTP POST, however, I don’t get any files in my public directory. An opmlfiles directory was created, but nothing ever gets there. Also, the filename is always blank in the model and database. What the heck am I doing wrong?

Update: SOLVED!!!!

BLARGH! The last problem was that my form needed to be defined as this:

<%= start_form_tag({ :action => 'create' }, :multipart => true) %>

and NOT this:

<%= start_form_tag(:action => 'create', :multipart => true) %>

Rails was erroring with “Do not know how to handle a string with value ‘hoyhoy.opml’ that was passed to a file_column” before I made the change.

Also, dopplertx was indeed correct, I needed to have a column in my database table for the filename. It is called file_column after all. Thanks everyone.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Ali Thu, 20 Apr 2006 20:39:53 UTC

    I got nothin’.

  2. Nate Wed, 28 Jun 2006 14:40:32 UTC

    Thanks for the update. That fixed it for me as well (the parentheses as well as the brackets)

  3. Nas Sat, 09 Sep 2006 15:46:01 UTC

    I am having a strange problem with image uploads, on the remote server file_upload uploads only gif images. If I try to upload JPG/PNG then it gives an error File invalid image but if I click on submit button again then it uploads it. However, it works well on my local machine which is a MAC.

    Any ideas??

  4. Tom Campbell Mon, 19 Nov 2007 02:35:52 UTC

    Per dopplertx, what type is the file_column field in the database?

  5. Jason Sun, 02 Dec 2007 13:42:12 UTC

    Thanks so much for this, I couldn’t figure out why the multipart attribute wasn’t being recognized until I found this post!!! -Jason

Comments