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

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

Related

Why is RSpec giving a NoMethodError when method exists?

I'm getting the error below
$ bundle exec rspec calculatorcli_spec.rb
Failure/Error: c = CalculatorCLI.parse
NoMethodError:
undefined method `parse' for CalculatorCLI:Class
from the below simple calculator class
CalculatorCLI is a class, and you're missing its initialization
either change CalculatorCLI.parse to CalculatorCLI.new.parseor changedef parsetodef self.parseinCalculatorCLI` definition

Figure out where a method was defined

If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)

Chef recipe gives error as undefined method 'split'

I am using this code in my Chef recipe. It works fine with all the other existing servers, however it doesn't work well with my new server:
user_array = node
node['user']['user_array_node_attr'].split("/").each do |hash_key|
user_array = user_array.send(:[], hash_key)
end
It returns an error:
FATAL: NoMethodError: undefined method 'split' for nil:NilClass
You don't have any value in
node['user']['user_array_node_attr'] #=> nil
You can check for the presence
node['user']['user_array_node_attr'].present? && node['user']['user_array_node_attr'].split("/").each do |hash_key|
user_array = user_array.send(:[], hash_key)
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 in Rails app

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 [].

Resources