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.
Related
I am having a problem that when ever I call the update function it throws an error. I have tried to find the solution but I could'nt find it and I can't understad the error. Please tell me what is wrong with the code
The update function is called from this function
def bookmark_request
data = params[:d]
request_bookmarked = Request.getRequest(data)
bookmarked_against_Request = Request.first
request_bookmarked_2 = request_bookmarked
bookmarked_against_Request_2 = bookmarked_against_Request
if bookmarked_against_Request_2[:favourites]
bookmarked_against_Request_2[:favourites] << bookmarked_against_Request[:id]
else
bookmarked_against_Request_2[:favourites] = Array.new
bookmarked_against_Request_2[:favourites] << bookmarked_against_Request[:id]
end
Request.updateRequest(bookmarked_against_Request , bookmarked_against_Request_2)
redirect_to :action => "active"
end
and the update code is this
def updateRequest(request,req_data)
if request.update(req_data)
request
end
end
The error that I am getting is this
**NoMethodError at requests/bookmark_request
undefined method `empty?' for Request:0x007f3fa44c59b0**
The error always comes on the line if request.update(req_data)
Sice I do not have a reputation of 10 so I am posting links to screenshot of the error
http://tinypic.com/r/whbiv7/8
update() method's argument is expected to be a hash. But your req_data argument becomes actually a Request here:
def bookmark_request
bookmarked_against_Request = Request.first
...
bookmarked_against_Request_2 = bookmarked_against_Request
...
end
And Request class has no empty? method. Moreover it might become nil, if there are no Requests at all.
I'm trying to filter the results that are returned from my index view based on optional params. My code is working for the first param, sinceDate. But for the second param, searchQeury, nothing is filtered out.
_controller.rb
def index
since = params[:sinceDate]
query = params[:searchQuery]
#articles = Comfy::Cms::Page.published.all
if since
#articles = #articles.reject{ |a| a[:created_at] < Date.parse(since) }
end
if query
#article = #articles.select{ |a| a[:label].match(/#{query}/i) }
end
end
Is it possible that the problem is a typo?
In the line after "if query", it should be perhaps #articles instead of #article.
I'm bringing in xml data to my application controller, passing to an array and then passing the data stored in this array to the view and into a highchart. Can anyone help please, I've the vast majority of it done, I just can't see what I'm doing wrong.
def index
#doc = Nokogiri::XML(open("http://api.worldbank.org/countries/BRA/indicators/1.0.HCount.2.5usd?per_page=10&date=1960:2014"))
#values = Array.new(9)
for i in 0 .. 8
#values[i] = #doc.xpath('//wb:value')
end
end
I then call the data in my series:
data: [<%=#values[0]%>, <%=#values[1]%>, <%=#values[2]%>, <%=#values[3]%>, <%=#values[4]%>, <%=#values[5]%>, <%=#values[6]%>, <%=#values[7]%>, <%=#values[8]%>]
The error message is as below:
undefined method `[]' for nil:NilClass
highlighted area where the error is:
data: [<%=#values[0]%>, <%=#values[1]%>, <%=#values[2]%>, <%=#values[3]%>, <%=#values[4]%>, <%=#values[5]%>, <%=#values[6]%>, <%=#values[7]%>, <%=#values[8]%>] –
On line 6, you forgot to indicate #doc.xpath('//wb:value'), so it should be:
#values[i] = #doc.xpath('//wb:value')
doc is instance variable.
For the array issue, you could use:
#doc = Nokogiri::XML(open("http://api.worldbank.org/countries/BRA/indicators/1.0.HCount.2.5usd?per_page=10&date=1960:2014"))
m = #doc.xpath("//wb:value").collect(&:text)
#values = Array.new
m.each do |m|
#values << m
end
My search returns this hash bellow
{"product"=>[{:title=>"It's All About Shoes Barbie Shoe Tree 2010 Hallmark Ornament"}, {:title=>"Iron Lady Bug Key Holder - Hide-A-Key"}]}
here is the loop and the code that generates the hash
id = "B003TM2IDS,B004X75EX4"
ids = id.split(',')
response = []
prod = Hash.new
product = Hash.new
#fetch product title from amazon
for aid in ids do
res = Amazon::Ecs.item_lookup(aid, { :response_group => "ItemAttributes"})
res.items.each do |item|
prod[:title] = item.get("ItemAttributes/Title")
end
# hash
product = {"product" => response.push(prod.dup)}
end
#loop to print the titles - Not working
product.each do |item_prod|
puts item_prod.title
end
I do get the
undefined method `title' for # (NoMethodError)
My question are:
Is the Product hash correct?
Is the loop correct?
I've done this millions of times but some reason I can't see the problem with this
Thanks a lot in advance
Do as below:
product["product"].each do |prod|
puts prod[:title]
end
product["product"].each { |p| puts p[:title] }
I have favorite user categories with causes. How can I get all causes from current_user.subscriptions ? Now I have code:
#categories = current_user.subscriptions
#causes = #categories.causes
but it returns
undefined method 'causes' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Category:0x0000000375d700>
Try this simple solution:
#categories = current_user.subscriptions
#causes = Cause.where(category_id: #categories)
you can use this query instead
#categories = current_user.includes(:subscriptions => :causes)
It includes every category and causes related to current_user