Forum: Ruby on Rails Newbie: Can't get file to upload

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.
Wes G. (Guest)
on 2006-03-13 19:14
Since I can't get the fancy upload progress thing to work, I thought
that I would revert back to a regular file upload scenario.

I have a very simple file upload form set up.  It appears that my file
parameter is coming into the controller as a string, not a file object.

Here's my view:

<%= form_tag  :action => 'save_HTML',
              :multipart => 'true' %>
  <%= file_field 'eSimplyJob', 'file' %>
  <P>
    <%= submit_tag "Upload" %>
  </P>
<%= end_form_tag %>


Here's my controller method:

 public
  def save_HTML
    @filename = @params[:eSimplyJob][:file].original_filename
    File.open("#{RAILS_ROOT}/htmlfiles/#{@filename}", "wb") do |f|
      f.write(@params[:eSimplyJob][:file].read)
    end
    puts "Finished with save_HTML"
  end

When I execute the upload, I get the following error:

undefined method `original_filename' for "upload.rhtml":String

I understand that this means that @params[:eSimplyJob][:file] is a
String and not an IO object.

What am I doing wrong?

Thanks for any help.

Wes G.
Wes G. (Guest)
on 2006-03-13 19:22
Well, I feel dumb.

I've decided to be very explicit about function call parens and the hash
specifications for parameters.

Doing this:

<%= form_tag ({ :action => 'save_HTML' },
              { :multipart => 'true' }) %>

works fine.

Thanks,
Wes

Wes G. wrote:
> Since I can't get the fancy upload progress thing to work, I thought
> that I would revert back to a regular file upload scenario.
>
> I have a very simple file upload form set up.  It appears that my file
> parameter is coming into the controller as a string, not a file object.
>
> Here's my view:
>
> <%= form_tag  :action => 'save_HTML',
>               :multipart => 'true' %>
>   <%= file_field 'eSimplyJob', 'file' %>
>   <P>
>     <%= submit_tag "Upload" %>
>   </P>
> <%= end_form_tag %>
>
>
> Here's my controller method:
>
>  public
>   def save_HTML
>     @filename = @params[:eSimplyJob][:file].original_filename
>     File.open("#{RAILS_ROOT}/htmlfiles/#{@filename}", "wb") do |f|
>       f.write(@params[:eSimplyJob][:file].read)
>     end
>     puts "Finished with save_HTML"
>   end
>
> When I execute the upload, I get the following error:
>
> undefined method `original_filename' for "upload.rhtml":String
>
> I understand that this means that @params[:eSimplyJob][:file] is a
> String and not an IO object.
>
> What am I doing wrong?
>
> Thanks for any help.
>
> Wes G.
John P. (Guest)
on 2006-03-15 07:33
Hey Wes,
Can you help me if I ask you to make the uploading restricted to only
doc files?
Since you are half way already, I think you'll be able to guess what I
am talking about.
Thanks in advance.
This topic is locked and can not be replied to.