【问题标题】:Getting no method error on attr_accessor在 attr_accessor 上没有方法错误
【发布时间】:2018-02-13 09:21:41
【问题描述】:

模型.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 获取值数组,但出现无方法错误。我是否错误地使用了 attr_accessor。

【问题讨论】:

  • 假设您的问题中的objectBaseTable 的一个实例......您当然应该打电话给object.valv 吗?此外,您的方法名称应以小写字符开头。类名大写。最后,以您实现DecoderArray 的方式,它已经为您返回@valv
  • 哦,伙计,我只需要重新启动控制台...我不知道为什么它不起作用。感谢您的帮助。

标签: ruby-on-rails attr


【解决方案1】:

当您编辑文件并打开rails console 时,您需要重新启动以将更改应用到控制台。要重启,可以使用Rails::ConsoleMethods#reload!的方法。

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

【讨论】:

  • 非常感谢@vnbrs
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 2023-03-29
  • 2012-11-09
  • 1970-01-01
相关资源
最近更新 更多