Answer the question
In order to leave comments, you need to log in
`
Why does the error occur: "undefined method `[]' for nil:NilClass"?
There is a field_pattern helper. In the vase I write:
field_pattern validation: {type: 'length', length: '50-200'}
def field_pattern data
validation = data[:validation]
validation
end
{:type=>"length", :length=>"3-100"}
validation[:type] # или validation[:length]
undefined method `[]' for nil:NilClass
validation.class
Hash
Answer the question
In order to leave comments, you need to log in
validation is a local variable inside the field_pattern function, it is not visible outside this function, which does not exclude the fact that you have some other variable / function with the same name in the scope in which you call the code that falls with an error
def field_pattern data
validation = data[:validation]
validation
end
v = field_pattern validation: {type: 'length', length: '50-200'}
v[:type] # "length"
v[:length] # "50-200"
def field_pattern data
$validation = data[:validation]
$validation
end
$validation[:type] # "length"
$validation[:length] # "50-200"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question