Carrierwave NoMethodError: undefined method `name 'for nil: NilClass:

There is already a similar question here Rails + CarrierWave: NoMethodError: undefined method 'name' for nil: NilClass , but the solution was to fix the typo.

I am already using Rails and Carrierwave on the same project with no problem. 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

      

Everything is as usual in the controller:

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 ahead. The following error appears when the form is submitted:

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 the name comes from. When you leave mount_uploader: upload, DataFileUploader in AR model everything works fine.

What's wrong here?

Thank you so much!

+3


source to share


2 answers


I had exactly the same error and the solution was to attach the loader to an existing field in my model. For your example, the fix would change from

class Upload < ActiveRecord::Base
  attr_accessible :title, :data_file, :caption
  mount_uploader :upload, DataFileUploader

  validates :title, :data_file, :presence => true
end

      

to



class Upload < ActiveRecord::Base
  attr_accessible :title, :data_file, :caption
  mount_uploader :data_file, DataFileUploader

  validates :title, :data_file, :presence => true
end

      

if you have a data_file field in the upload model and don't have a upload field (examining the db / schema.rb file will be helpful).

+5


source


I couldn't find a solution to why the above code doesn't work, but I created a new model called DataFile and a new uploader called FileUploadUploader. It really works. So my guess is there might be a naming conflict because I named the model loading. But this is actually just a guess ...



0


source







All Articles