Parsing with Nokogiri: "each" and "content", works but gives error - ruby-on-rails

I use Nokogiri gem to parse website with 'each' and 'content' methods. It's complicated parser with several usings of those methods. For some reason at some point task fails with error. Undefined method 'content' for nillclass. And gives me references for all lanes I use those classes in. It's not regular fatal error, If I try to put result of those content it prints result needed but still crashes. Here is error I get:
rake aborted!
undefined method `content' for nil:NilClass
/mnt/shoppe/lib/tasks/parser.rake:50:in `block (6 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:49:in `block (5 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:47:in `block (4 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:27:in `block (3 levels) in <top (required)>'
/mnt/shoppe/lib/tasks/parser.rake:12:in `block (2 levels) in <top (required)>'
I'm new to ruby and RoR. Maybe there some type of error which only crash program if some number of this errors reached? Couldn't find mention of something like this, most likely I don't know the key word to look for. So any help will be useful. Thank You.

Undefined method 'content' for nil:NilClass.
This error means, at some point in your rake task, when you call something.content, your something is nil.
To avoid this issue, you can call content using try:
something.try(:content)
That way, your program won't crash if some of the somethings are nil.

Usually this means that your input varies at some point. For example, you're parsing an array of nodes in a loop and trying to access get content from a link, something like this:
<div>test</div>
<div>test</div>
<div></div>
Note the third div. If you're expecting in your code that div always contains a link, your code will fail on the div without link, since link will be nil and trying to get content on nil gives you an error you have.
To workaround it you may use link.try(:content) instead of calling link.content directly.

Related

ruby module NameError: uninitialized constant error

I am trying to create a gem, but when trying to load a module in test I get the following error. I used "Configurable Ruby gems: Custom error messages and testing" to set environment variables from users and that's where most of the code is from.
1) Msg91sms::Configuration with configuration block returns the correct authkey
Failure/Error: raise Errors::Configuration, "Msg94 auth key missing!" unless #authkey
NameError:
uninitialized constant Msg91sms::Configuration::Errors
# ./lib/msg91sms/configuration.rb:10:in `authkey'
# ./spec/msg91sms/configuration_spec.rb:7:in `block (3 levels) in <top (required)>'
but as per the folder structure and everything this should be Msg91sms::Errors::Configuration. I put only one here even though all tests are failing due to improper module loading.
The gem with this error can be found here: https://github.com/flyingboy007/msg91sms/tree/development
bundle exec rspec will throw all errors.
It should be something with naming or improper loading. But I can't figure out.
After following the answer by #sergio, I am now getting this error:
1) Msg91sms::Configuration with configuration block returns the correct authkey
Failure/Error: raise ::Msg91sms::Errors::Configuration, "Msg91 auth key missing!" unless #authkey
NameError:
uninitialized constant Msg91sms::Errors
# ./lib/msg91sms/configuration.rb:10:in `authkey'
# ./spec/msg91sms/configuration_spec.rb:7:in `block (3 levels) in <top (required)>'
Could someone tell me what am doing wrong here?
raise Errors::Configuration, "Msg94 auth key missing!" unless #authkey
Use fully qualified name to help ruby lookup the class.
raise ::Msg91sms::Errors::Configuration, "Msg94 auth key missing!" unless #authkey
The folder structure is like Errors::Configuration but the error is showing like Configuration::Errors..Dont know why..
It's trying to find Errors::Configuration within Msg91sms::Configuration (the current scope at that point). But since there's no Msg91sms::Configuration::Errors, it fails with that message.

attr_accessible error on rails 4.1.8 upon performing rake db:migrate on heroku

I am still learning Ruby (so I am a complete noob),
right now I have my app successfully running locally but when trying to opening the apps on heroku , in which I first perform the heroku run rake db:migrate I stumbled upon a problem.. it tells me :
Running `rake db:migrate` attached to terminal... up, run.2149
-- attr_accessible(:pName, :pQuantity, :pMeter, :pWeight, :pSellPrice, :pCategory, :pPic)
-- attr_accessible(:pName, :pQuantity, :pMeter, :pWeight, :pSellPrice, :pCategory, :pPic)
rake aborted!
NoMethodError: undefined method `attr_accessible' for #<ActiveRecord::Migration:0x007f2dc2ba45b8>
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:648:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:641:in `method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:406:in `method_missing'
/app/db/migrate/20150802134246_create_inventories.rb:2:in `<class:CreateInventories>'
/app/db/migrate/20150802134246_create_inventories.rb:1:in `<top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `block in require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:232:in `load_dependency'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:761:in `load_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:757:in `migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:752:in `disable_ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1044:in `use_transaction?'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:954:in `rescue in block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:951:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
I have been trying to find out the reason, after wondering around I found out about change in rails 4.0.0 in that attr_accessible are no longer used and we should use strong parameter instead, So removing the attr_accessible from model will solve the problem...
However, I have an empty Model, there is no attr_accessible everywhere i look. (beside this is weird why my apps runs locally but not on heroku?)
I can't figured out why this error appear and where to look for solutions.. I have been trying to look at active_record file but am afraid of making any changes, any idea?
also, could anyone tell me any resources that can help me read this type of log errors? I have tried to read some articles but can't find one that is easy to understand for noobs like me... ;(
Glad I could help :)
As far as reading log errors goes, here's what I do. You're going to get a bunch of lines about files you have never touched. That's because although there's most likely an error in something you wrote, the error propagates through the entire Rails framework. That's why Rails is really cool: it does a lot of things for you, so you don't have to worry about them.
If the error is in code you wrote, that file should also be listed in the stack trace. So in this example, you have a bunch of /app/vendor/bundle/... (which I assumed you didn't touch), but then there is an /app/db/migrate/... (which is most likely something you created).
That leads me to think the error is in that file. Plus, it even gives you a line number (that's what the :2 is in create_inventories.rb:2). Granted those line numbers aren't always correct, but it should get you started in the right place!

Get the Failure/Error line from rspec output in after(:each)

I'm using TestRails to log failures in our automated test suite. Example failures have output like this:
Failures:
1) can click on the Photos button after logging in
Failure/Error: login.signin_btn.click
NoMethodError:
undefined method `click' for nil:NilClass
# /Users/kkrzeminski/TestApp/Appium/Common/pages_helper.rb:36:in `click'
# ./test_1_spec.rb:9:in `block (2 levels) in <top (required)>'
I can grab the exception using example.exception in the after(:each) block in my spec_helper, as well as the backtrace, but what I'm really interested is that line beginning with Failure/Error:. I can't seem to find a way to get that string. It would be handy for logging the reason for test failure in TestRails, as just the exception and the backtrace aren't very descriptive.
It sounds like what you really want is a custom formatter for your CI environment. The protocol for RSpec formatters provides an example_failed hook which will give you the example object.
You can then use Example and Notifications objects to gather the information you are desire.

Rails Console Error

When I open Rails console, or sandbox for that matter, and I try and add new date, in this case, a user, I keep getting this same error. I have tried everything to get it resolved, but for the life of me I cannot figure it out. I am using Windows, so obviously I know that is one of my first problems, but its all I have for the current moment, so I will have to make due. Here is the error message that pops up when I try and simply enter user.new(information...)... any help would be greatly appreciated, I am pulling my hair out over here.
C:\TTS\Rails\workspace\sample_app>rails console
DL is deprecated, please use Fiddle
Loading development environment (Rails 4.2.0)
irb(main):001:0> user.new(name: "Will Wagar", email: "will#wagar.com")
NameError: undefined local variable or method `user' for main:Object
from (irb):1
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
You almost certainly want User.new (note the capital U) rather than user.new, it's a Ruby convention that classes are capitalised.
To expand, you probably want to do it in several steps:
user = User.new(name: "Will Wagar", email: "will#wagar.com")
user.valid?
user.save
etc...
So in the first one you're creating an instance of the class User and assigning it to the variable user and then in subsequent operations, you're working with the new user variable you've created.
I see that now, I should have mentioned in the question that I have tried it both ways, capitalized and not. I can get a return when I try it with a capital U, but when I try and check to see if it is valid or save it, I get the same error.
irb(main):008:0> user.valid?
NameError: undefined local variable or method user' for main:Object
from (irb):8
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:instart'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:inconsole'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in run_command!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in'
from bin/rails:4:in require'
from bin/rails:4:in'

How to disable build-in Test

I created my model whitch is Test, and everything is working ok when I am using it within browser, but when I try to do something with it from rails c, I got error.
1.9.3p125 :001 > t=Test.find(4)
NoMethodError: undefined method `find' for Test:Module
from (irb):1
from /home/dorijan/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /home/dorijan/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /home/dorijan/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
So is there a way to disable this temporary? I know I shouldnt use this, but now it is too late...:(
Thank you
I dont know how to disable the Test module in rails console, sorry.
One workaround is to namespace your model into a new module and then at the console call from within its new name space see how to here
Problem may be that you have used 'test' which is on the rails reserved word wiki as possibly problematic see
rails reserved words
Try another model name not on reserved list

Resources