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:
Ruby on Rails - File Uploading | Tutorialspoint
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
File Upload
<% form_tag :action => 'uploadFile', :multipart => true do %>Select File :
<%= file_field ‘upload’, ‘datafile’ %>
<%= submit_tag “Upload” %>
<% end %>
run application
http://upload.datajazz.com/upload/index
Error Output
NoMethodError in UploadController#uploadFile
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
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”}