Forum: Ruby on Rails trouble with file_column plugin

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Roberto S. (Guest)
on 2005-11-15 00:05
(Received via mailing list)
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' %>
<label for="upload_filename">File: </label><br/>
<%= file_column_field("book", "file") %><br/><br/>
<label for="title">Title</label><br/>
<%= text_field 'book', 'title' %><br/><br/>
<label for="amount">Price in U$</label><br/>
<%= text_field 'book', 'amount' %><br/><br/>
<%= 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
skanthak (Guest)
on 2005-11-15 12:28
(Received via mailing list)
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
This topic is locked and can not be replied to.