I've implemented file upload with carrierwave in at least 5 different places across my app. They all work well. Until the most recent implementation... on save in the create method I get the following exception:
NoMethodError (undefined method
name' for nil:NilClass):
create'
app/controllers/downstream_documents_controller.rb:34:in
I can't figure this one out. The code was pulled from a previous file upload which currently works just fine. I simply changed the model names appropriately.
Here is my Model code:
class DownstreamDocument < ActiveRecord::Base
attr_accessible :company_id, :created_by, :updated_by, :document_title, :document_type_id, :downstream_id, :file_name, :is_active
# set schema name and table name for TakebackDB
self.table_name = "track.DownstreamDocument"
mount_uploader :file_name, DownstreamDocumentUploader
default_scope where(:is_active => true)
belongs_to :company, :foreign_key => "company_id"
end
And uploader:
# encoding: utf-8
class DownstreamDocumentUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
include CarrierWave::MimeTypes
# Choose what kind of storage to use for this uploader:
storage :azure
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"#{model.class.to_s.underscore}/#{mounted_as}/#{model.company_id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
def cover
manipulate! do |frame, index|
frame if index.zero?
end
end
# Create different versions of your uploaded files:
version :thumb do
process :cover
process :resize_to_limit => [100, 100]
process :convert => 'jpg'
process :set_content_type
def full_filename (for_file = model.source.file)
super.chomp(File.extname(super)) + '.jpg'
end
end
# Change Image to JPEG
def set_content_type(*args)
self.file.instance_variable_set(:@content_type, "image/jpg")
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png pdf)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
And my controller:
class DownstreamDocumentsController < ApplicationController
load_and_authorize_resource
respond_to :html, :js
def index
@downstream_id = params[:downstream_id]
@downstream = Company.find(@downstream_id)
@documents = DownstreamDocument.where(:company_id => current_user.company_id, :downstream_id => @downstream_id )
end
def create
@downstream_document = DownstreamDocument.new(file_name: params[:file])
@downstream_document.created_by = current_user.user_name
@downstream_document.updated_by = current_user.user_name
@downstream_document.downstream_id = params[:downstream_id]
@downstream_document.company_id = params[:company_id]
@downstream_document.document_title = params[:file]
@downstream_document.document_type_id = 1
@downstream_document.is_active = params[:is_active]
if @downstream_document.save!
#do some stuff
@downstream = Company.find(@downstream_document.downstream_id)
@documents = DownstreamDocument.where(:company_id => current_user.company_id, :downstream_id => @downstream_id )
# render json: { message: "success", company_profile_document: @company_profile_document,
# company_profile: @company_profile }, :status => 200
# else
# render json: { error: @image.errors.full_messages.join(',')}, :status => 400
# end
respond_to do |format|
format.json{ render :json => @documents }
end
end
end
def update
end
def delete
end
end
The exception is thrown in the controller's create method on if @downstream_document.save!
All the variables for @downstream_document appear to be populated correctly prior to the save call.
If I've got a typo or syntax error, I'm not seeing it. Any help would be appreciated!
Thanks in advance... I've been staring at this for way too long and need a new pair of eyes on it.
Aucun commentaire:
Enregistrer un commentaire