There is already a similar question here Rails + CarrierWave: NoMethodError: undefined method `name' for nil:NilClass but the solution there was to fix a typo.
I am already using Rails and Carrierwave in the same project with no problems. There is a simple AR model:
class Upload < ActiveRecord::Base
attr_accessible :title, :data_file, :caption
mount_uploader :upload, DataFileUploader
validates :title, :data_file, :presence => true
end
In the controller thers is as usual:
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
Straight forward. When submitting the form the following error occures:
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 (?, ?, ?, ?, ?)
I don’t see the error and don’t understand, where name comes from. When leaving away mount_uploader :upload, DataFileUploader in the AR model, everything works fine.
What is wrong here?
Thanks a lot!
I had exactly the same error and the solution was to connect uploader to existing field in my model. For your example the fix will be to change from
to
if you have data_file field in Upload model and don’t have upload field (examining your db/schema.rb file will be helpful).