[英]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
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.