NoMethodError in Rails app - ruby

I am new to Rails (using 3.2.9) and get a NoMethodError I don't know how to fix. Can anyone help, please?
A NoMethodError occurred in trade_plans#update:
undefined method `[]' for false:FalseClass
app/models/trade_plan.rb:96:in `symbol_is_valid'
And this is line 96 in trade_plan.rb:
if(data[:last_trade_price_only] == "N/A" || data[:last_trade_price_only].blank?)
Any ideas why this error occurs and how to fix it?
Thanks :)

That's probably because your local variable data has the value false instead of being an instance of Hash.
Since you are trying to call the method [] on the object false, it raises a NoMethodError because false does not respond to [].

Related

undefined method `gsub' for nil:NilClass ruby

i am a newbie in ruby... I'm trying to convert media into a scorm package using what i found on github but i got an error while trying to run the script in the command prompt undefined method `gsub' for nil:NilClass. I guess it may be due to a defined method. any idea on how i can remove this error ?
dir = ARGV.shift.gsub(/\/+$/, '')
index = nil
media = []
Dir["#{dir}/media/*.json"].each do |file|
id = JSON.parse(File.read(file))
base = file.gsub(/\/media\/.*\.json$/, '')
index = "#{base}/index.html"
name = File.basename file
media.push [name,id]
puts "#{name}: #{id}"
end
As the error says, you are calling the method gsub on an object that is an instance of NilClass, in other words you are calling gsub on nil.
The error message tells you in which method and on which line this happens and how you got to that line of code. You will have to examine the error message to find the place in your code where you are calling gsub on an object that is nil, then you have to examine your code to find out why that object is nil instead of a String as you expect it to.

undefined method constantize in rake task

I'm trying to run this code"
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.constantize.each do |factory|
factory.public_send(:delete_all)
end
end
But I get this error: NoMethodError: undefined methodconstantize' for #`
Do you know how I can solve the issue?
In ruby uppercased variables are constants by default so you can't call constantize on it. In your case it's just an array so this should work:
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.each do |factory|
factory.constantize.public_send(:delete_all)
end
You can call String#constantize only on strings but you are calling it on array FACTORY.
Remove FACTORY.constantize and add factory.constantize.public_send(:delete_all)
Also make sure you have ActiveSupport::Inflector required

Ruby 2.1 and mislav-will_paginate 2.3.10 shows ERROR: NoMethodError: undefined method `paginate'

Does anyone know what I'm doing wrong here, I installed Ruby 2.1, Sequel-4.26.0 gem and mislav-will_paginate-2.3.10 gem, but when i try to use the paginate function, i keep getting the following error:
Code:
#user = User.paginate(:page => 1, :per_page => 2)
Error message:
"ERROR: NoMethodError: undefined method `paginate' for #"
Most likely paginate is a dataset method, not a class method (this is true for Sequel's pagination extension, not sure about will_paginate). If you want User.paginate to work:
def User.paginate(*args)
dataset.paginate(*args)
end

Ruby. Crack gem. -- in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) --

Dear stackoverflow community,
Beginner's question:
Why do I get the following error?
scraper_sample_2.rb:7:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
>Exit code: 1
Here's my code (copied from a ruby's intro guide):
require "rubygems"
require "crack"
require "open-uri"
URL = "http://www.recovery.gov/pages/GetXmlData.aspx?data=recipientHomeMap"
Crack::XML.parse(open(URL).read)["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end
Because Crack::XML.parse(open(URL).read)["totals"] is nil. Try to split the call you do on line 7 on several lines and debug each call separately. Maybe the answer you get is not what you expect.
Given the format of the xml returned from your source, Crack::XML.parse(open(URL).read)["totals"] will, as Ivaylo said, return nil. The format of the xml must have changed, as totals are now within /map/view.
To get the expected output, change your code to:
Crack::XML.parse(open(URL).read)["map"]["view"]["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end

NoMethodError .to_i ruby

A basic code in ruby that isn't working for me.... The error specifies
NoMethodError (undefined method `to_i' for
row = row.split(",").map { |x| x.to_i }
EDIT:
NoMethodError (undefined method `to_i' for [["123,123,123,"]]:Array):
app/controllers/sessions_controller.rb:21:in `import'
app/controllers/sessions_controller.rb:20:in `each'
app/controllers/sessions_controller.rb:20:in `import'
app/controllers/sessions_controller.rb:17:in `each'
app/controllers/sessions_controller.rb:17:in `import'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.2ms)
you're receiving that error because you're calling the method .to_i on an array, which is wrong since that method is of the String class, source :http://www.ruby-doc.org/core-2.1.0/String.html#method-i-to_i
So in order to fix this we need to take that string from the array so we can turn it into an integer, remember that you started with an array containing an array, so that's why I'm calling .pop twice:
row = [["123, 123, 123"]]
string_of_numbers = row.pop.pop
string_of_numbers.split(",").map {|x| x.to_i }
If it feels too odd try to play with it on irb, that's usually how I try to sort these kinds of things out, don't forget to always refer to the official documentation

Resources