How can I add ID method/attribute to a dialog hash? This is require because there might be more than one item and each needs an ID or key.
I get the error NoMethodError: undefined method id' for #Hash:0x000055b16edb60a8`.
dialog_hash = {}
response["value"].each_with_index do |mgmt_group, index|
id = index
value = mgmt_group
dialog_hash[mgmt_group.id] = "#{value}"
end
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
I have defined in analysis_result.rb this:
def total_matches
...
end
and I am trying to use it in a view _rable_row.haml like this:
- if analysis.results.total_matches != 0
= link_to analysis.title, analysis, class: 'js-toggle', data: { href: "loading-#{analysis.id}" }
- elsif analysis.results.total_matches == 0
= render partial: 'partials/shared/empty'
but I'm getting undefined method 'total_matches' for #<Mongoid::Criteria:
Can someone tell me why is this happening?
The error undefined method 'total_matches' arises because total_matches cannot be called directly on the results array, it is an attribute of objects present inside the results array. I.e. you are trying to call an attribute of an object on array of objects, instead of on the object itself.
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 was using the following piece of code without any issues. the function querytags returns a set of "products". geturl function checks if a product has an image associated with it. All products with an image are pushed to productsProxy array
#query = params[:tag]
#products = queryTags(#query)
#productsProxy = Array.new
if #products != nil
#products.each do |p|
tempProduct = ProductProxy.new(p)
if tempProduct.getUrl(tempProduct.images[0]['id'], 'small', tempProduct.images[0]['file'])
#productsProxy.push(tempProduct)
end
end
else
#productProxy = []
end
Then i tried to add another parameter in the URL and changed the querytags function accordingly.
#query = params[:tag]
#taxon = params[:taxon]
#products = queryTags(#query, #taxon)
#productsProxy = Array.new
if #products != nil
#products.each do |p|
tempProduct = ProductProxy.new(p)
if tempProduct.getUrl(tempProduct.images[0]['id'], 'small', tempProduct.images[0]['file']) #now showing error
#productsProxy.push(tempProduct)
end
end
else
#productProxy = []
end
But I stated getting undefined method[]' for nil:NilClass` on the line:
if tempProduct.getUrl(tempProduct.images[0]['id'], 'small', tempProduct.images[0]['file'])
I checked with the help of debugger that #products array is not empty. I am unable to figure out why suddenly i am getting this error. please can someone help
It's not about the "#products" array being empty or not - if it was empty, the "#products" iterator wouldn't do anything. The problem is that one of your products is either lacking an images attribute or the images[0] is not returning a hash. For debugging purposes I'd start by adding "break if p.images.nil?" to the top of your iterator and go from there.