I have no idea what the problem is on this. I'm trying this in the rails console:
>> agent = Mechanize.new
NoMethodError: undefined method `user' for nil:NilClass
I added gem 'mechanize' and did a bundle install. I did require 'mechanize'. I also tried require 'nokogiri' below that, and every time I get the same error. I've tried 4 or 5 different ways to instantiate a new mechanize object and haven't been able to find one.
I searched my entire rails app for the string '.user' and couldn't find any. Not sure what the deal is here. Any ideas?
Thanks
EDIT
Full Stack trace
NoMethodError: undefined method `user' for nil:NilClass
from /home/me/.rvm/gems/ruby-1.9.3-p448#my_app/gems/net-http-persistent-2.9.2/lib/net/http/persistent.rb:866:in `proxy='
from /home/me/.rvm/gems/ruby-1.9.3-p448#my_app/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:1189:in `set_proxy'
from /home/me/.rvm/gems/ruby-1.9.3-p448#my_app/gems/mechanize-2.7.2/lib/mechanize.rb:204:in `initialize'
from (irb):5:in `new'
from (irb):5
from /home/me/.rvm/gems/ruby-1.9.3-p448#global/gems/railties-3.2.15/lib/rails/commands/console.rb:47:in `start'
from /home/me/.rvm/gems/ruby-1.9.3-p448#global/gems/railties-3.2.15/lib/rails/commands/console.rb:8:in `start'
from /home/me/.rvm/gems/ruby-1.9.3-p448#global/gems/railties-3.2.15/lib/rails/commands.rb:41:in `<top (required)>'
from /home/me/Dropbox/Work/RubymineProjects/apps/my_app/script/rails:6:in `require'
from /home/me/Dropbox/Work/RubymineProjects/apps/my_app/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
You're not doing anything wrong: the version of mechanize that came out a few days ago is buggy. I solved that error just updating the gem (i.e., with bundle update mechanize).
Related
I am trying to run a single test file: bundle exec rspec spec/models/user_spec.rb
But get following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller
NameError:
uninitialized constant Rails::Controller
Did you mean? ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
Initially, I was getting following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel
NameError:
uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
But then it fixed after I added following to the test file.
require "shoulda/matchers"
I am newbie in Ruby/Rails world, can someone please give a direction?
Rails::Controller::Testing::TemplateAssertions was removed in Rails 5.
You can re-add the depreciated functionality by installing the Rails controller testing gem. However the use of controller specs, assigns and template assertions is discouraged by both the RSpec and Rails teams and is not very future proof.
The community accepted solution is to write request specs and stop poking inside your controllers.
I am following the Kevin Skoglund tutorial for Ruby on Rails called Ruby on Rails 4 Essential Training. In the section 'Create Record' I am having the following error after simply trying to create a record:
George$ pwd
/Users/George/Sites/simple_cms
George$ rails console
Loading development environment (Rails 4.2.0)
irb(main):001:0> subject = Subject.new
NameError: uninitialized constant Subject
from (irb):1
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/George/Sites/simple_cms/bin/rails:8:in `<top (required)>'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/George/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
irb(main):002:0>
As I am still in the early stages of learning I cannot establish what the cause might be although it appears to be gem related based on the errors return.
If anyone has any suggestion and maybe a tip on how I can work out what the error and fix is for myself it would be appreciated as I really want to keep learning.
George$ pwd
/Users/George/Sites/simple_cms
George$ rails console
Loading development environment (Rails 4.2.0)
> rails generate model Subject name:string type:string
> rake db:migrate
> rails console
> sub = Subject.new
Try this.
I'm using the rails-settings gem: https://github.com/huacnlee/rails-settings-cached
The getter/setter method are not working.
I've added the following line to my user model as directed:
include RailsSettings::Extend
I've also tried adding it to role.rb (as I am using rolify gem)
However is am still getting the following error message:
NoMethodError: undefined method `settings' for #<User:0x007f8dd017a560>
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/rolify-3.2.0/lib/rolify/role.rb:73:in `method_missing'
from (irb):24
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/jamieturner/.rvm/gems/ruby-2.0.0-p353#accufly/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
When I run:
user = User.find(2)
user.settings.color = :red
Can anyone see what the issue is?
Ok, I've solved the issue.
I had not restarted the console session.....restarting it got it working!
I'm using Rails 3.2.12 and Roby 2.0.0-p0 in RVM etc. In rails console I get the following error after doing u = Users.first:
u = Users.first
Loading production environment (Rails 3.2.12)
2.0.0p0 :001 > u = Users.first
NoMethodError: undefined method `first' for Users:Module
from (irb):1
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p0#discourse/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I was able to access the database and view the record via the psql console using the user rails would be using.
If you already have a User model defined, try User.first instead of Users.first.
I am writing a class method to find certain data in my database however I am getting a NoMethodError: undefined method error. I am using Datamapper instead of ActiveRecord. Here is my error output:
NoMethodError: undefined method `get_courses_starting_in' for Course:Class
from /Users/hugo/.rvm/gems/ruby-1.9.2-p318/gems/dm-ar-finders-1.2.0/lib/dm-ar-finders.rb:155:in `method_missing_with_find_by'
from /Users/hugo/Web Development/Rails/connectedtrips/lib/ct_gems/dm-core-1.2.0/lib/dm-core/model/relationship.rb:372:in `method_missing'
from /Users/hugo/Web Development/Rails/connectedtrips/lib/ct_gems/dm-core-1.2.0/lib/dm-core/model/property.rb:249:in `method_missing'
from (irb):24
from /Users/hugo/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/hugo/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/hugo/.rvm/gems/ruby-1.9.2-p318/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>'
And here is my class method:
def self.get_courses_starting_in
courses = []
Course.all.each{ |course| courses << course if course.days_till_course_starts == 7}
return courses
end
I hope someone can help. Thanks.
Have you write the code after starting the console? Restart console and check again.