【发布时间】:2014-11-30 17:54:58
【问题描述】:
用例:上传和处理一个 csv 文件,并在数据库中记录上传会话。
方法:创建一个模型来保存有关上传会话的数据,并创建一个控制器,其中包含一个收集 csv 文件的新方法和一个创建、填充和保存上传对象的 create 方法。
问题:模型包含一个初始化方法(参见模型代码),该方法似乎具有初始化对象所需的信息(参见调试输出),但是,当控制器尝试使用模型的新方法时,该尝试没有产生一个有用的对象(参见控制器代码和调试输出)。
问题:创建 call_history_upload 对象需要进行哪些更改?
Ruby 1.9.3 on Rails 3.2.13 on Windows 8.1
型号代码:
class CallHistoryUpload < ActiveRecord::Base
attr_accessible :file_name, :user_id, :record_count, :international_call_count, :unknown_client_count
has_many :call_histories, dependent: :destroy
require 'csv'
def initialize( file_name, user_id )
logger.debug( "CallHistoryUpload.initialize start")
logger.debug( " call_history_file = " + file_name )
logger.debug( " user_id = " + user_id )
@file_name = file_name
@user_id = user_id
@record_count = 0
@international_call_count = 0
@unknown_client_count = 0
logger.debug( "CallHistoryUpload.initialize end")
end
end
控制器代码:
class CallHistoryUploadsController < ApplicationController
def get_client_id
-1
end
# GET /call_history_uploads/new
# GET /call_history_uploads/new.json
def new
@call_history_upload = CallHistoryUpload.new( "Unknown", session[:user_id] ) # Needed to suppoprt json
respond_to do |format|
format.html # new.html.erb
format.json { render json: @call_history_upload }
end
end
# POST /call_history_uploads
# POST /call_history_uploads.json
def create
logger.debug( "CallHistoryUploadsController start")
@call_history_upload = CallHistoryUpload.new( params[:call_history_file].original_filename, session[:user_id] )
logger.debug( "CallHistoryUploadsController after instantiation")
<<This is line 24>>logger.debug( "call_history_upload.file_name = " + @call_history_upload.file_name )
respond_to do |format|
if @call_history_upload.save
format.html { redirect_to @call_history_upload, notice: 'Call history upload was successful.' }
format.json { render json: @call_history_upload, status: :created, location: @call_history_upload }
else
format.html { render action: "new" }
format.json { render json: @call_history_upload.errors, status: :unprocessable_entity }
end
end
end
end
日志输出:
Started POST "/call_history_uploads" for 127.0.0.1 at 2014-11-30 08:40:27 -0500
Processing by CallHistoryUploadsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iDugtKa8b/U9q71Gk86sJMK6hnX1gvgQt496PX2q4Oo=", "call_history_file"=>#<ActionDispatch::Http::UploadedFile:0x4717788 @original_filename="Test_kiyvivzokpysxilfoftavyuftot.csv", @content_type="application/vnd.ms-excel", @headers="Content-Disposition: form-data; name=\"call_history_file\"; filename=\"Test_kiyvivzokpysxilfoftavyuftot.csv\"\r\nContent-Type: application/vnd.ms-excel\r\n", @tempfile=#<File:C:/Users/Gene/AppData/Local/Temp/RackMultipart20141130-5144-yn8i9>>, "commit"=>"Submit"}
CallHistoryUploadsController start
CallHistoryUpload.initialize start
call_history_file = Test_kiyvivzokpysxilfoftavyuftot.csv
user_id = KinteraAdmin
CallHistoryUpload.initialize end
CallHistoryUploadsController after instantiation
Completed 500 Internal Server Error in 0ms
NoMethodError (undefined method `has_key?' for nil:NilClass):
app/controllers/call_history_uploads_controller.rb:24:in `create'
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (0.0ms)
【问题讨论】:
标签: ruby-on-rails ruby