Hi All,

As I suspected, it was a very simple mistake in the original code
which I had picked up from the web.
Issue: the destination path didn't contain a valid directory.
I suspect the original code probably worked on a different platform
(probably Windows).

Here is the complete working solution, which I have rewritten to
eliminate some of the older rails code.

Regards
************ Working solution
### dir: [app/models] file: data_file.rb
class DataFile < ActiveRecord::Base
  def self.save(upload)
    # Changed Default Destination: [__RAILS_DIR__/public/data]
    name = "data/" + upload.original_filename
    File.open(name, "wb") { |f| f.write(upload.read) }
  end
end

### dir: [app/controllers] file: upload_controller.rb
class UploadController < ApplicationController
  def index
     render :file => 'upload/uploadfile.rhtml'
  end

  def uploadFile
    file_param = params[:upload][:datafile]
    post = DataFile.save(file_param)
    render :text => "File has been uploaded successfully"
  end
end

### dir: [app/views/upload] file: uploadfile.rhtml
<h1>File Upload</h1>
<% form_for :upload,:url=>{:action => 'uploadFile'},:html =>
{ :multipart => true } do |f| %>

<p><label for="upload_file">Select File</label> :
<%= f.file_field 'datafile' %></p>
<%= f.submit "Upload" %>
<% end %>

### run application
http://upload.datajazz.com/upload/index


On Jul 20, 7:40 am, GuruOne <[email protected]> wrote:
> Hi All,
>
> Please help. I am sure there is something obvious I am overlooking in
> the code but I cannot see it. Please note, as detailed below, I am on
> Bluehost using Rails 2.2.2.
>
> The code is example code from TutorialsPoint
> - URL:http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm
> and modified to run on Linux bluehost.
>
> On bluehost
> -http://upload.datajazz.com/
> gives standard 'Welcome Page'
> -http://upload.datajazz.com/upload/index
> presents html page for file upload
> upload file selection works
> failure occurs on 'Upload' button click
>
> Issue Summary:
> - 'nil' whereas it should be variables being passed from
> uploadfile.rhtml
> - Line 3 in [app/models] data_file.rb
>     name =  upload['datafile'].original_filename # Line 3
> - Line 7 in [app/controllers] upload_controller.rb
>     post = DataFile.save(params[:upload]) # Line 7
>
> Regards
>
> ### Environment information
> Hosting service: Bluehost
>
> ### Rails Environment
> Ruby version    1.8.6 (x86_64-linux)
> RubyGems version        1.3.1
> Rails version   2.2.2
> Active Record version   2.2.2
> Action Pack version     2.2.2
> Active Resource version 2.2.2
> Action Mailer version   2.2.2
> Active Support version  2.2.2
> Application root        ~/rails/upload
> Environment     development
> Database adapter        sqlite3
> Database schema version 0
>
> ### Relevant source files
> ### dir: [app/models] file: data_file.rb
> class DataFile < ActiveRecord::Base
>   def self.save(upload)
>     name =  upload['datafile'].original_filename # Line 3
>
>     directory = "public/data"
>     # create the file path
>     path = File.join(directory, name)
>     # create or over-write file 'path' with 'datafile' contents
>     File.open(path, "wb") { |f| f.write(upload['datafile'].read) }
>   end
> end
>
> ### dir: [app/controllers] file: upload_controller.rb
> class UploadController < ApplicationController
>   def index
>      render :file => 'upload/uploadfile.rhtml'
>   end
>
>   def uploadFile
>     post = DataFile.save(params[:upload]) # Line 7
>     render :text => "File has been uploaded successfully"
>   end
>
> end
>
> ### dir: [app/views/upload] file: uploadfile.rhtml
> <h1>File Upload</h1>
> <% form_tag :action => 'uploadFile',
>                         :multipart => true do %>
> <p><label for="upload_file">Select File</label> :
>
> <%= file_field 'upload', 'datafile'  %></p>
>
> <%= submit_tag "Upload" %>
> <% end %>
>
> ### run applicationhttp://upload.datajazz.com/upload/index
>
> ### Error Output
> NoMethodError in UploadController#uploadFile
>
> You have anilobject when you didn't expect it!
> You might have expected an instance of ActiveRecord::Base.
> The error occurred while evaluatingnil.[]
> RAILS_ROOT: ~/rails/upload
>
> Application Trace | Framework Trace | Full Trace
> app/models/data_file.rb:3:in `save'
> app/controllers/upload_controller.rb:7:in `uploadFile'
>
> Request
> Parameters:
>
> {"multipart"=>"true",
>  "commit"=>"Upload",
>  "authenticity_token"=>"82b3eeae67198fb71bb4dea43b1790ac70bda0ae"}
> Show session dump
>
> Response
>
> Headers:
>
> {"cookie"=>[],
>  "Content-Type"=>"",
>  "Cache-Control"=>"no-cache"}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to