I am receiving a very strange error. undefined method `delete' for 0:Fixnum when I try to style my rows. Why is this the case?
table(headers + Invoice.all.map do |invoice|
[
make_cell(content: invoice.invoice_number),
make_cell(content: format_date(invoice.invoice_date)),
make_cell(content: format_date(invoice.due_date)),
make_cell(content: format_price(invoice.total)),
make_cell(content: format_price(invoice.fees_total)),
make_cell(content: format_price(invoice.actual_total))
]
end, table_options, style(cell(0), padding: [4,2]))
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 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.
def function1
#user=User.find(params[:user_id])
#participants=Participant.find_all_by_user_id(params[:user_id])
bar_1_data = #user.get_total
bar_2_data = params[:count]
color_1 = 'c53711'
color_2 = '0000ff'
min=0
max=100
puts(bar_1_data) # prints 2 on console
puts(bar_2_data) # prints 3 on console
puts (bar_1_data).is_a? Integer # prints true
puts (bar_2_data).is_a? Integer # prints false
bc=GoogleChart::BarChart.new("300x80", " ", :horizontal, false)
bc.data " ", [100], 'ffffff'
bc.data "Bar1", bar_1_data, color_1
bc.data "Bar2", bar_2_data.to_i, color_2
bc.axis :x, :range => [min,max]
bc.show_legend = true
bc.stacked = false
bc.data_encoding = :extended
#bc= bc.to_url
end
end
I am getting the argument error "comparison of String with 100 failed" int the above controller code. Then I changed the lines
bc.data "Bar1", bar_1_data, color_1
bc.data "Bar2", bar_2_data, color_2
to
bc.data "Bar1", bar_1_data.to_i, color_1
bc.data "Bar2", bar_2_data.to_i, color_2
which gives me undefined method collect for 2:Fixnum at line #bc= bc.to_url.
On console it gives the following error:
NoMethodError (undefined method `collect' for 2:Fixnum):
gchartrb (0.8) lib/google_chart/base.rb:499:in `extended_encode'
gchartrb (0.8) lib/google_chart/base.rb:461:in `encode_data'
gchartrb (0.8) lib/google_chart/bar_chart.rb:69:in `process_data'
gchartrb (0.8) lib/google_chart/bar_chart.rb:68:in `collect'
gchartrb (0.8) lib/google_chart/bar_chart.rb:68:in `process_data'
gchartrb (0.8) lib/google_chart/base.rb:440:in `add_data'
gchartrb (0.8) lib/google_chart/base.rb:305:in `prepare_params'
gchartrb (0.8) lib/google_chart/base.rb:77:in `to_url'
app/controllers/assess_controller.rb:139:in `function1'
gchartrb (0.8) lib/google_chart/bar_chart.rb:22:in `initialize'
app/controllers/assess_controller.rb:131:in `new'
app/controllers/assess_controller.rb:131:in `function1'
/Library/Ruby/Gems/1.8/gems/hoptoad_notifier-2.4.11/lib/hoptoad_notifier/rack.rb:27:in `call'
/Library/Ruby/Gems/1.8/gems/hoptoad_notifier-2.4.11/lib/hoptoad_notifier/user_informer.rb:12:in `call'
Rendering rescues/layout (internal_server_error)
Tried printing bc in the view as <p><img src="<%= puts (#bc) %>" ></p>,it doesn't print anything, but when I am printing bc in the controller it prints
#<GoogleChart::BarChart:0x103be9660>.
Am I not sending it correctly to the view? I have tried both #bc=bc.to_url and puts bc.to_url as in http://gchartrb.rubyforge.org/.
What am I missing?
Without knowing the api for google charts, I see this:
bc.data " ", [100], 'ffffff'
bc.data "Bar1", bar_1_data, color_1
bc.data "Bar2", bar_2_data.to_i, color_2
The parameters of the first line are of type String, Array, String.
The parameters of the next two are String, Fixnum, String
Your error message is undefined method collect for 2:Fixnum ... so perhaps you should change the Fixnum to Array.
bc.data " ", [100], 'ffffff'
bc.data "Bar1", [bar_1_data], color_1
bc.data "Bar2", [bar_2_data.to_i], color_2
It looks like you're loading bar_1_data and bar_2_data into arrays. Thus the error undefined method to_i for Array.
Instead of:
bar_1_data = [#user.get_total]
bar_2_data = [params[:count]]
Just try
bar_1_data = #user.get_total
bar_2_data = params[:count]
Note that putting your data inside [] brackets means it's now an array of one item.
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?