I have following code and getting error of "undefined method each' for nil:NilClass"
#loan_ids = params[:moredata].split(",").map { |s| s.to_i }
#loans_ids.each do |number|
puts number
end
Its #loan_ids.each not #loans_ids.each
#loan_ids=params[:moredata].split(",").map{|s| s.to_i }
#loan_ids.each do |number|
puts number
end
You have misspelled it
It's #loan_ids.each not #loans_ids.each. You have a plural "loans" when it's not defined that way.
Related
I have a helper method which returns array
def site
return Website::SITE.collect!{ |arr| arr if arr[1] != 'site_builder' }
end
Website::SITE return array in console
I call this method in view.
- site.each do |menu|
tr
td= menu[0]
Here it gives ActionView::Template::Error (undefined method `[]' for nil:NilClass):
Let's redefine the function like this:
def site
Website::SITE.compact.select{ |arr| arr if arr[1] != 'site_builder' }
end
It's because, the array Website::SITE contains nil value.
[["About ", "about"], ["Calendar", "calendar"], ["Gallery", "gallery"], ["Information", "information"], ["Manage ", "manage"], nil, ["Template", "website"]]
If you didn't put the method in the helpers folder but directly to the controller, then you have to add a line of code after your method to make it work.
def site
return Website::SITE.collect!{ |arr| arr if arr[1] != 'site_builder' }
end
helper_method :site
This should fix the error
Here is the code
class Tech
attr_reader :content, :tech_hash
#tech_hash = Hash.new(0)
def initialize(content)
#content = content
showTech(content)
end
def showTech(content)
content.split.each do |word|
#tech_hash[word] += 1
end
#tech_hash = #tech_hash.sort_by{|k,v| -v}.to_h
p #tech_hash
end
end
class Digital
def analyze()
File.foreach('test.txt') do |content|
ob = Tech.new(content)
end
end
end
digi = Digital.new()
digi.analyze()
Here is the error
D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:10:in `block in showTech': undefined method `[]' for nil:NilClass (NoMethodError)
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:9:in `each'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:9:in `showTech'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:6:in `initialize'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:22:in `new'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:22:in `block in analyze'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:20:in `foreach'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:20:in `analyze'
from D:/graded-assignments/course01/module02/assignment-Calc-Max-Word-Freq/Tech.rb:29:in `<main>'
#tech_hash is defined outside of an instance method, what makes it a class variable. Therefore within showTech there is still no #tech_hash instance variable defined and therefore it returns nil
Just move #tech_hash into the initialize method to initialize an instance variable:
def initialize(content)
#content = content
#tech_hash = Hash.new(0)
showTech(content)
end
I'm parsing JSON data to save to my db.
The JSON data is like this:
{"id"=>889066,
"email"=>"new.user#email.com",
"created_at"=>"2014-10-24T18:46:13Z",
"updated_at"=>"2014-10-24T18:46:13Z",
"status"=>"Registered",
"custom_status"=>nil,
"first_name"=>New,
"last_name"=>User,
"latest_visitor"=>
{"id"=>16998604, "tracking_code"=>"cab237f6-50ec-4424-9ea9-b0110070a6cb"},
"url"=>{"id"=>2422287, "url"=>"http://www.website.com/"},
"referrer"=>{"id"=>4234808, "url"=>"https://www.google.ba/"},
"affiliate"=>nil,
"campaign"=>nil,
"search_term"=>
{"id"=>344901, "term"=>"puppies", "search_engine"=>"google"},
"tracking_code"=>"cab237f6-50ec-4424-9ea9-b0110070a6cb"}]
For example I want to get the search engine value, so I do:
json = JSON.parse(response.body)
json.each do |item|
Model.create(
search_engine: item.fetch("search_term").fetch("search_engine")
)
end
This returns the error:
in 'block in <top (required)>': undefined method 'fetch' for nil:NilClass (NoMethodError)
EDIT: Here is the output of puts item.keys:
id
email
created_at
updated_at
status
custom_status
first_name
last_name
latest_visitor
url
referrer
affiliate
campaign
search_term
tracking_code
What am I doing wrong?
What you want to do is detect when the search_term is nil and do something about it.
If you want to discard it, that's easy
json = JSON.parse(response.body)
json.each do |item|
search_term = item.fetch("search_term")
next if search_term.nil?
Model.create(
search_engine: search_term.fetch("search_engine")
)
end
If you want to provide some default search_engine value when there are no search_term:
DEFAULT_SEARCH_ENGINE = "N/A"
json = JSON.parse(response.body)
json.each do |item|
search_term = item.fetch("search_term")
if search_term.nil?
search_engine = DEFAULT_SEARCH_ENGINE
else
search_engine = search_term.fetch("search_engine")
end
Model.create(
search_engine: search_engine
)
end
Im writing a simple statement which should check whether values in an array contain a 7.
I had the following in mind:
def checkforseven(an_array)
newArray = []
an_array.each do |num|
if num.include?(7)
newArray << num
end
end
newArray
end
array = [1,2,14,27]
exclaim(array)
But this not seem to work... Am getting a "nomethod error"
NoMethodError: undefined method `include?' for 1:Fixnum
Any thoughts on how I can solve this?
NoMethodError: undefined method `include?' for 1:Fixnum
As i said,the include should be used with an array.Currently you are iterating an_array and using include on its elements,which is wrong.
Try this
def checkforseven(an_array)
newArray = []
if an_array.include?(7)
newArray << an_array
end
newArray
end
I am trying to run the following code:
dupe_groups = Activity.all.group_by { |e| e.non_id_attributes }.select{ |gr| gr.last.size > 1 }
redundant_elements = dupe_groups.map { |group| group.last - [group.last.first] }.flatten
redundant_elements.each(&:destroy)
However, I get the following error:
Activity.find(:all).group_by { |e| e.non_id_attributes }.select{ |gr| gr.last.size > 1 }
NoMethodError: undefined method `last' for #<Hash:0x00000107e505e8>
from (irb):10:in `block in irb_binding'
from (irb):10:in `select'
from (irb):10
from /usr/local/bin/irb:12:in `<main>'
How can I get this fella to work?
When you do a group_by you get a hash, the thing you group by is represented as the keys in the hash so when you select over it you should be doing .select{|key, values| ...} and you can then values.size > 1
Although, when I look at this code, it has a smell to me. What are you actually trying to do?