Forum: Ruby on Rails File is nil

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.
29237231571cf4eefd412948688386da?d=identicon&s=25 Lisa (Guest)
on 2006-06-07 03:30
I don't understand why my program does not get the file that I try to
upload.
It returns the following error when I try to upload a small file:

  NoMethodError in LogfileController#create

  You have a nil object when you didn't expect it!
  The error occured while evaluating nil.original_filename

  #{RAILS_ROOT}/app/models/logfile.rb:9:in `file='
  #{RAILS_ROOT}/app/controllers/logfile_controller.rb:31:in `create'

  Request
  Parameters: {"logfile"=> {"fileName"=>#<StringIO:0x39c3fc0>},
               "commit"=>"Create"}


This is the code in view:

   <%= form_tag({:action => 'simple_save'},
                {:method => "post", :multipart => true}) %>
      <input type="file" name="logfile_fileName" />
      <%= submit_tag "Simple Save" %>
   <%= end_form_tag %>

This is the code in the controller:

   def create
      ...
      @logfile.file= @params['logfile_fileName']
      ...
   end

And the Model is nearly identical to the code in here:

   http://www.bigbold.com/snippets/posts/show/1805

Is it because I don't have a file field in my database?  The database
keeps the name of the file and some information related to it.  I want
to save the file locally only, not to the database.

Please help!  I've been stuck for DAYS.

Thank you in advance.
29237231571cf4eefd412948688386da?d=identicon&s=25 Lisa (Guest)
on 2006-06-07 04:29
Oh. My. Goodness.

What was missing was the the length of character input the html.
So I went overboard with the input size.

My view now reads:
<input type="file" id="logfile_file" name="logfile[file]" size="200"/>
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-06-07 12:55
(Received via mailing list)
Hi --

On Wed, 7 Jun 2006, Lisa wrote:

>  #{RAILS_ROOT}/app/controllers/logfile_controller.rb:31:in `create'
>
>  Request
>  Parameters: {"logfile"=> {"fileName"=>#<StringIO:0x39c3fc0>},
>               "commit"=>"Create"}
[...]
>      @logfile.file= @params['logfile_fileName']

Those don't match.  Try:

   @logfile.file = params['logfile']['fileName']


David

--
David A. Black (dblack@wobblini.net)
* Ruby Power and Light, LLC (http://www.rubypowerandlight.com)
   > Ruby and Rails consultancy and training
* Author of "Ruby for Rails" from Manning Publications!
   > http://www.manning.com/black
Eeba234182bcbd7faed9ff52e233394d?d=identicon&s=25 Douglas Livingstone (Guest)
on 2006-06-07 12:55
(Received via mailing list)
Here's your problem:

2006/6/7, Lisa <maneneko@yahoo.com>:
>
>   Request
>   Parameters: {"logfile"=> {"fileName"=>#<StringIO:0x39c3fc0>},
>                "commit"=>"Create"}


>       @logfile.file= params['logfile_fileName']

You're tring to access the 'logfile_fileName' key of params, but
params only has a 'logfile' and a 'commit' key. But, the param
'logfile' is itself a hash, with a key 'fileName', so what you really
want is:

@logfile.file= params['logfile']['fileName']

When you have an underscore in the param name, Rails groups the params
by prefix. Here, the prefix is 'logfile', so you get a hash saved
under 'logfile' with all the params which start 'logfile_', rather
than accessing the params 'logfile' itself.

This is especially useful when, say, you want to create two model
objects, and you want to initialise them from a hash. This way you can
have fileds 'alpha_name', 'alpha_address' and 'beta_name' and
'beta_age', which will give you two hashes, params['alpha'] and
params['beta']. Have a look in the documentation under form helpers to
learn more about how this works.

hth,
Douglas
D707194949c58eb4076f040adf116508?d=identicon&s=25 Bruno Celeste (Guest)
on 2006-06-07 13:02
(Received via mailing list)
It's not `@params['logfile_fileName']` but
`params[:logfile]["fileName"]`
29237231571cf4eefd412948688386da?d=identicon&s=25 Lisa (Guest)
on 2006-06-07 20:24
Thank you all so much!

Everyone in the Ruby community has been so helpful.

I have to admit this is scaffold withdrawal, but I honestly tried to
read the documentation (I just didn't know where to start).  Much of it
is still magic and mystics to me, but I hope to become better at it.

Thanks again.

Lisa
This topic is locked and can not be replied to.