【发布时间】:2012-03-06 23:45:23
【问题描述】:
Rails + CarrierWave: NoMethodError: undefined method `name' for nil:NilClass 这里已经有一个类似的问题,但解决方案是修复一个错字。
我已经在同一个项目中使用 Rails 和 Carrierwave,没有任何问题。有一个简单的 AR 模型:
class Upload < ActiveRecord::Base
attr_accessible :title, :data_file, :caption
mount_uploader :upload, DataFileUploader
validates :title, :data_file, :presence => true
end
在控制器中和往常一样:
def create
@upload = Upload.new(params[:upload])
if @upload.save
redirect_to new_admin_upload_path, :notice => t("site.successfully_created_resource")
else
render :action => 'new'
end
end
直截了当。提交表单时出现以下错误:
ActiveRecord::StatementInvalid in Admin::UploadsController#create
NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "uploads" ("caption",
"created_at", "data_file", "title", "updated_at") VALUES (?, ?, ?, ?, ?)
我没有看到错误,也不明白名称的来源。在 AR 模型中离开 mount_uploader :upload、DataFileUploader 时,一切正常。
这里有什么问题?
非常感谢!
【问题讨论】:
-
谢谢 - 但实际上我已经提到了这篇文章,但它没有提供解决方案....