未定义的方法'detailsFileTag'为零:NilClass
我的目标是测试一个设计模式 - 在这种情况下是装饰器模式。未定义的方法'detailsFileTag'为零:NilClass
我采取了lib/
文件夹和controller
文件夹下面的代码。 这里的库文件有一个BasicTag
类,继承类TaggingDecor
和另一个类DescriptionDecor
。
现在所有这三个类都有一个共同的功能,称为detailsForTag
。 当从控制器文件调用此功能时,如下所示。 该错误是
未定义的方法`detailsFileTag”的零:NilClass
和误差线是如在下面的代码标记。
# the concrete component we would like to decorate class BasicTag
def initialize()
@init_basicTag = "File with no tags"
end
# getter method
def detailsFileTag
return " test from BasicTag....."
end
end
# decorator class -- this serves as the superclass for all the concrete decorators
# the base/super class decorator (i.e. no actual decoration yet), each concrete decorator (i.e. subclass) will add its own decoration
class TaggingDecor
def initialize(r_file)
@r_file = r_file
@aTagg = "no tag added"
@aDescTag = "no description added"
end
# override the details method to include the description of the extra feature
def detailsFileTag
return @aTagg + @aDescTag
end
end
# a concrete decorator
class TagDec < TaggingDecor
def initialize(r_file)
super(r_file)
@aTagg = "no tag added"
@aDescTag = "tagdec description added"
end
# override the details method to include the description
def detailsFileTag
return @aTagg + @aDescTag
end
end
# another concrete decorator
class DescriptionDec < TaggingDecor
def initialize(r_file)
super(r_file)
@aTagg = "no tag added"
@aDescTag = "descriptiondec description added"
end
# override the details method to include the description
def detailsFileTag
return @aTagg + @aDescTag
end
end
============================= controller file ============================
# POST /file_taggings
# POST /file_taggings.json
def create
@file_tagging = FileTagging.new(file_tagging_params)
#####################################################
#rails g scaffold fileTagging filename:string filetag:string filedescription:string
# information For logging the file details into table
@file_tagging.filename = params[:file_tagging][:filename]
@file_tagging.filetag = params[:file_tagging][:filetag]
@file_tagging.filedescription = params[:file_tagging][:filedescription]
########################################################################
# information For logging the file details
# create an instance/object of a BasicTag
myTagg = BasicTag.new
# add the extra features to the new car
if params[:file_tagging][:filetag].to_s.length > 0 then
myTagg = TagDec.new(myTagg)
end
#if params[:file_tagging][:description].to_s.length > 0 then
# myTagg = DescriptionDec.new(myTagg)
#end
## populate the Description details - By calling the BASE class and SUPER class as stated above.
#Error is here for the call myTagg.detailsFileTag !
@file_tagging.filedescription = myTagg.detailsFileTag
# retrieve the instance/object of the MyLogger class
logger = MyLogger.instance
logger.logInformation("A new file details are: " + @file_tagging.filesdescription)
logger.logInformation("A new file details are: ")
#####################################################
respond_to do |format|
if @file_tagging.save
format.html { redirect_to @file_tagging, notice: 'File tagging was successfully created.' }
format.json { render :show, status: :created, location: @file_tagging }
else
format.html { render :new }
format.json { render json: @file_tagging.errors, status: :unprocessable_entity }
end
end
end
============================================================
回答:
是的纠正它的一个n object的对象。所以我错误地删除了模型文件夹中由脚手架创建的ruby文件。 model >> FileTagging.rb。
以上是 未定义的方法'detailsFileTag'为零:NilClass 的全部内容, 来源链接: utcz.com/qa/258006.html