2017-12-02 193 views
-3

我的目标是测试一个设计模式 - 在这种情况下是装饰器模式。未定义的方法'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 
============================================================ 
+0

嗨,欢迎来到Stack Overflow。我看到你的代码(这很好)......但我仍然有点困惑......问题是什么?你尝试过什么,什么不适合你? –

+0

错误是说你正在'nil'上调用'detailsFileTag'。鉴于你唯一调用该方法的地方在'myTagg'上,这意味着'myTagg'由于某种原因是'nil'。您是否尝试过使用调试器来遍历代码,并查看每个步骤中“myTagg”的值?当它被初始化时是否正确初始化? –

回答

0

是的纠正它的一个n object的对象。所以我错误地删除了模型文件夹中由脚手架创建的ruby文件。 model >> FileTagging.rb。

相关问题