简体   繁体   English

错误(为nil:NilClass调用了私有方法“ select”):

[英]Error (private method `select' called for nil:NilClass):

Why would I be getting this error? 为什么会出现此错误? How do I fix it? 我如何解决它?

ActionView::Template::Error (private method `select' called for nil:NilClass): ActionView :: Template :: Error(为nil:NilClass调用了私有方法“ select”):

class MyformObject

  include ActiveModel::Model

  #validations


@form_structure =
  [
    {  name: :country, model: :car, field_type: :string },
    {  name: :street, model: :repair_center, field_type: :string }
  ]

  def get_model_from_name(name)
    model = @form_structure.select { |record| record[:name] == name }.first[:model]
  end  

  def type_for_attribute(name)
    get_model_from_name(name).to_s.capitalize.constantize.type_for_attribute
  end 

  def has_attribute?(name)
    get_model_from_name(name).to_s.capitalize.constantize.attributes.key?(name.to_s)
   end

def self.fields_of_model(model)
  form_structure.select { |record| record[:model] == model }.map { |record| record[:name] }
end

 end

UPDATED VERSION: 更新后的版本:

ERROR: undefined method `[]' for nil:NilClass def get_model_from_name(name) 错误:nil的未定义方法“ []”:NilClass def get_model_from_name(name)
model = self.class.form_structure.select { |record| 模型= self.class.form_structure.select {|记录| record[:name] == name }.first[:model] end record [:name] == name} .first [:model]结尾

class MyformObject

  include ActiveModel::Model

  #validations


def self.form_structure 
  [
    {  name: :country, model: :car, field_type: :string },
    {  name: :street, model: :repair_center, field_type: :string }
  ]
end

  def get_model_from_name(name)
    model = self.class.form_structure.select { |record| record[:name] == name }.first[:model]
  end  

  def type_for_attribute(name)
    get_model_from_name(name).to_s.capitalize.constantize.type_for_attribute
  end 

  def has_attribute?(name)
    get_model_from_name(name).to_s.capitalize.constantize.attributes.key?(name.to_s)
   end

def self.fields_of_model(model)
  form_structure.select { |record| record[:model] == model }.map { |record| record[:name] }
end

def self.delegate_fields_to(*models)
      models.each do |model|
        fields_of_model(model).each do |attr|
          delegate attr.to_sym, "#{attr}=".to_sym, to: model
        end
      end
    end

 end

IMHO, a constant is a good way to handle that array, as is full of hardcoded values, and it contains just two elements. 恕我直言,常量是处理该数组的好方法,因为它充满了硬编码的值,并且只包含两个元素。

So you can do something like: 因此,您可以执行以下操作:

class MyformObject
  FORM_STRUCTURE = [
    { name: :country, model: :car, field_type: :string },
    { nname: :street, model: :repair_center, field_type: :string }
  ].freeze
  private_constant :FORM_STRUCTURE

  def get_model_from_name(name)
    FORM_STRUCTURE.select { |record| record[:name] == name }.first[:name]
  end 
end

p MyformObject.new.get_model_from_name(:country)
# [:country]

There you can see that the constant can't be modified and is only being used within the MyformObject scope. 在那里您可以看到常量不能被修改,只能在MyformObject范围内使用。

I deleted the model local variable assignment as it's not being used. 我删除了模型局部变量赋值,因为它没有被使用。

You can try like this which make little easy to execute, 您可以尝试这样操作,但执行起来很困难,

  def get_model_from_name(name)
    model = form_structure.select { |record| record[:name] == name }.first[:model]
  end

  def form_structure
    [
      {  name: :country, model: :car, field_type: :string },
      {  name: :street, model: :repair_center, field_type: :string }
    ]
  end

And for better encapsulation, you can put method form_structure in private scope. 为了更好地封装,您可以将form_structure方法form_structure私有范围内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoMethodError:私有方法`warn&#39;调用nil:NilClass - NoMethodError: private method `warn' called for nil:NilClass Redmine-调用Dispatcher.dispatch#时出错<NoMethodError: private method `split' called for nil:NilClass> - Redmine - Error calling Dispatcher.dispatch #<NoMethodError: private method `split' called for nil:NilClass> Pik安装Ruby Yields:错误:私有方法&#39;gsub&#39;调用nil:NilClass - Pik Install Ruby Yields: Error: Private method 'gsub' called for nil:NilClass 私有方法`update_without_password&#39;调用了nil:NilClass - private method `update_without_password' called for nil:NilClass 私有方法“ main_image”是否需要nil:NilClass? 怎么修 - private method `main_image' called for nil:NilClass? how to fix Rails错误:未定义的方法`private_method_defined?&#39; 为零:NilClass - Rails Error: undefined method `private_method_defined?' for nil:NilClass 在collection_select上为nil:NilClass定义未定义的方法`map&#39;-调用的引用可能为nil - Rails undefined method `map' for nil:NilClass on collection_select - The reference called may be nil 未定义的方法,用于nil:NilClass错误 - undefined method for nil:NilClass error 如何使用HtmlFormatter运行RSpec? 获取“私有方法`puts”调用nil:NilClass“ - How to run RSpec using HtmlFormatter? Getting “private method `puts' called for nil:NilClass” 是什么导致在Rails迁移期间私有方法`gsub`调用nil:NilClass? - What is causing private method `gsub` called for nil:NilClass during Rails migration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM