Trouble with file_column plugin


#1

I can’t get the file_column plugin 0.3 working propperly (on Win XP,
with
rails 0.14.3) . Without validation and form redisplay, everythings seems
to
work fine, by when I do a form redisplay, then the file-column field is
nil
after saving the record, however i have in the hidden formfield the key
of
the already uploaded file a temporary location. Here are the relevant
snippets of my code:

Model

class Book < ActiveRecord::Base
belongs_to :user
file_column :file
validates_presence_of :title, :amount
validates_numericality_of :amount
end

Controller


def create_ebook
return unless request.post?
uploaded_file = params[:book][:file]

@book = Book.new(:file => uploaded_file,
:size => uploaded_file.size,
:title => params[:book][:title],
:amount => params[:book][:amount],
:created_at => Time.now)

current_user.books << @book
if current_user.save
if @ebook.file.nil?
@book.destroy
flash[:notice] = “#{@book[‘file’]} upload failed”
else
flash[:notice] = “#{@book[‘file’]} uploaded successfully”
end
redirect_to :action => ‘list’
else
@user = current_user
end
end

View

<%= form_tag( { :action => “create_book” }, { :multipart => true } ) %>
<%= error_messages_for ‘book’ %>
File:

<%= file_column_field(“book”, “file”) %>


Title

<%= text_field ‘book’, ‘title’ %>


Price in U$

<%= text_field ‘book’, ‘amount’ %>


<%= submit_tag ‘Upload’ -%>
<%= end_form_tag %>

Has anybody an idea what I am doing wrong or has a working example of
file-column code to share ?

thanks in advance


#2

On 11/14/05, R. Saccon removed_email_address@domain.invalid wrote:

belongs_to :user
uploaded_file = params[:book][:file]

@book = Book.new(:file => uploaded_file,
                  :size => uploaded_file.size,
                  :title => params[:book][:title],
                  :amount => params[:book][:amount],
                  :created_at => Time.now)

well, you aren’t passing the hidden field (which is called
‘book[file_temp]’) to the book model. The shortest way would be
something like: “@book = Book.new(@params[:book])” as it will
automatically include all params, but you might have to be careful
about security in this case.

If you want to keep your current style, you should add the following
after creating the @book object:

@book.file_temp = @params[:book][:file_temp] if
@params[:book][:file_temp]

Hope this helps
Sebastian