Reputation: 306
<%
@loans.each do |loan_rec|
loan=loan_rec.info
if loan['_LoanName'].blank?
loan_name = 'No Loan Title'
else
loan_name = loan['_LoanName']
end
end
%>
I am getting error of undefined method `[]' for nil:NilClass
. What is undefined? and when I print loan
array in a loop, it gives the following result:
{
"_LendingCategory"=>"Private Real Estate Loan",
"Email"=>"[email protected]",
"FirstName"=>"TestDavid",
"Id"=>3573,
"_LoanName"=>"null",
"LastName"=>"TestGeorge",
"_DesiredTermLength"=>"3",
"_TransactionType0"=>"Purchase",
"_CashContribution"=>100000.0,
"_NetLoanAmountRequested0"=>8000000.0
}
Upvotes: 0
Views: 78
Reputation: 32933
You need to anticipate that there might not be an info
for every loan_rec
. I would refactor your code like this:
<%
@loans.each do |loan_rec|
loan = loan_rec.info
loan_name = loan && loan['_LoanName']
if loan_name.blank?
loan_name = 'No Loan Title'
end
end
%>
Upvotes: 1
Reputation: 1705
In your loan
, there is no any info
key. So you would have at aleast one info
value.
Upvotes: 1
Reputation: 2589
You have at least one Loan
for which loan_rec.info
is nil
. You can try:
loan = loan_rec.info || Loan.new
as a quick fix, but you should also figure figure out how to avoid the nil
in the first place.
Upvotes: 3