简体   繁体   中英

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):

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)
model = self.class.form_structure.select { |record| record[:name] == name }.first[:model] end

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM