ROR + Error nil. Way? when running code

My Project table has an invoice as an integer attribute. Here I put the null object in this attribute in the DB. During the evaluation of nil.empty? occurs. HAML broach code

- @project.each do |proj|
  =proj.invoice if !proj.invoice.blank? || !proj.invoice.empty? || !proj.invoice.nil?
  - @project_invoice=proj.invoice

  =@project_invoice=0 if proj.invoice.blank? || proj.invoice.empty? || proj.invoice.nil

I get this error when running code.

NoMethodError: you have a null object when you did not expect this! Perhaps you were expecting an array instance. Error evaluating nil.empty?

+3
source share
4 answers

Here are a few standard tests provided by Ruby and rails that may help you, but you usually don't need to use all at once:

# Rails provided Object#blank? method
nil.blank? # => true
false.blank? # => true
''.blank? # => true
[ ].blank? # => true

# Ruby provided Object#nil? method
nil.nil? # => true
false.nil? # => false
''.nil? # => false
[ ].nil # => false

# Ruby class-specific #empty? method
nil.empty? # => error
false.empty? # => error
''.empty? # => true
[ ].empty? # => true

, , , , . blank? present?, . :

- @project_invoice = proj.present? ? proj.invoice : 0

:

- if (proj.present?)
  @project_invoice = proj.invoice
- else
  @project_invoice = 0

, -, - .

+4

. proj, projt.

.

0

- , ! proj.invoice.blank? false, , projt.invoice.empty?

- , nil.empty? , ? .

ruby-1.9.2-p0 > !nil.blank? || !nil.empty?
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.empty?

, overkill, . , , blank?, .

0
source

This is the code you are using. If this is the same code, I see a spelling error in the second line.

Can you try with

=proj.invoice if !proj.invoice.blank? || !proj.invoice.empty? || !proj.invoice.nil?
0
source

All Articles