attr_accessor上没有方法错误

时间:2018-02-13 09:21:42

标签: ruby-on-rails attr

Model.rb

class BaseTable < ApplicationRecord
before_save :ConverterArray
serialize :conF_string
attr_accessor :valv

def initialize(valv)
    @valv = valv
end
#  def self.Conf_count  # self. is used when u have to call the method on te whole class example User.Conf_count
#      Confirmer.count
#  end

def ConverterArray  # self. is not used when u have to call the method on the instance variable for example user = User.new, user.ConverterArray
    count = Confirmer.count
    index = Array.new(count, 0)
    conf = conF_string.map(&:to_i)
    conf.each do |n|
        index[n] = 1
    end
    index = index*""
    self.conF_string = index
end

def DecoderArray
    count = Confirmer.count
    value = []
    conf = conF_string.split("")
    i = 0
    conf.each_with_index do |n,index|
        if n=="1"
            value[i] = index
            i+=1
        end
    end
    @valv = value
end
end

尝试使用rails控制台中的object.DecoderArray.valv来获取值数组,但是获取no方法错误。我是否错误地使用了attr_accessor。

1 个答案:

答案 0 :(得分:0)

每当您编辑文件并打开url(r'^swagger/', TemplateView.as_view(template_name='swagger.html'), name='swagger'), 时,都需要重新启动才能将更改应用到控制台。要重新启动,您可以使用方法Rails::ConsoleMethods#reload!

您只需在rails console中运行reload!即可。