One of my rspec controller tests is failing with
undefined method `current_user' for #<#:0x007fbf18554608>
This is strange because the line where the error occurs looks like this:
h.respond_to?(:current_user) ? h.current_user : nil) : 0
Before I access current_user, I check its presence with respond_to?
When I run the tests with a debugger this happens:
(byebug) dh.respond_to?(:current_user)
true
(byebug) dh.current_user
*** NoMethodError Exception: undefined method `current_user' for #<# <Class:0x007f9bfed902f0>:0x007f9bf346fc08>
nil
How is it possible that the Object answers to respond_to?(:current_user) with true and returns a no method error when I try to call the method?
It also very difficult to create a minimal example that fails, because the tests are only failing im I run the complete suite. Running the failing tests standalone succeeds.
I guess your ruby version must be less than Ruby 2.0. Till Ruby 1.9, respond_to? method returns true for protected and private methods. But when a protected or private method is invoked on a object, that raises NoMethodError.
http://tenderlovemaking.com/2012/09/07/protected-methods-and-ruby-2-0.html
In your case, current user must be either private or protected method.
Related
I've followed this guide:
http://guides.rubyonrails.org/plugins.html#add-an-acts-as-method-to-active-record
In fact, I've actually copied the code and put into my app and it doesn't work.
I keep getting undefined method errors.
When I use console, I can check if the module is initialized and it is, but the model that uses the "acts_as" method throws undefined method errors.
Also in console I can call ActiveRecord::Base.send :include, ... explicitly and then it includes the module -- then everything works. But try as I may, I can't get rails to call .send on ActiveRecord::Base at load time.
Any ideas?
I am using rails 3.2.16 and ruby 1.9.3
I get this error from the localhost browser after rails s.
undefined method `to_date' for nil:NilClass
like this
NoMethodError in Products#new
Showing /Users/main/railscasts-episodes/episode-350/store-
after/app/views/products/_form.html.erb where line #27 raised:
undefined method `to_date' for nil:NilClass
in irb> mode it works only when I
require 'date'
Now my question is about my rails project. In which file in my project I should add
require 'date'
I added it into my model it did not work. Or, maybe I need to install any specific gem? to make this work
The problem you have isn't an issue with to_date, it's a problem with the object you're trying to perform the method on
Your issue will either be:
You have not declared the variable / object
Your variable / object is not populated with any data
I'd highly recommend posting the controller action you're using to invoke the to_date method. Obviously, there may be an issue with the way you're calling it, but the immediate issue is you're trying to deal with an unpopulated variable
I am trying execute test cases with rspec version 2.14 for which I am getting following error
undefined method `rspec_reset' for
I am trying to use rspec_reset on the class. The same test cases are working with rspec 2.13.1. So is it possible that rspec_reset method is not available after 2.13?
The reset method does not exist in RSpec 2.14.x. Instead, it is a helper method defined inside the spec_helper.rb file for the rspec-mocks project.
module VerifyAndResetHelpers
def verify(object)
RSpec::Mocks.proxy_for(object).verify
end
def reset(object)
RSpec::Mocks.proxy_for(object).reset
end
end
You can see that this method delegates the reset action to the underlying proxy instead of tacking it on to the class definition of the object in question.
Yes, in 2.14, rspec_reset is no longer available on all objects as it was previously, as discussed in https://github.com/rspec/rspec-mocks/pull/250.
Although I can't find any documentation on it, there now appears to be an RSpec class method reset which takes an object as an argument and will effectively "undo" any RSpec operations that have been done to that object.
There's an RSpec "example" at https://github.com/rspec/rspec-mocks/blob/cee433c89125a3984df33c87eb61985613adce9b/spec/rspec/mocks/mock_spec.rb which still uses the rspec_reset in the description of the example, but which now uses the aforementioned reset method to do the reset. In earlier versions of the example, the reset was done with rspec_reset.
I want to put something in the log, but I keep getting
NameError: undefined local variable or method 'logger' for Report:Class
I've tried
logger "hi"
logger 'hi'
logger.info 'hi'
Rails.logger "hi"
but I just get the above error.
I tried adding config.logger = Logger.new(STDOUT) to my config/environments/development.rb but that didn't help, still gettng the error.
I am running the method in script/console... but I exit and enter it each time to reload everything.
You have to use:
Rails.logger.info 'hi'
Rails.logger returns a logger instance, and by calling info, warn, error or debug on it with a message, this message is logged with the specified log level.
Within an ActiveRecord or ActionController instance you also have a logger convenience accessor available that returns Rails.logger.
Unless you have a variable or method called logger available in the scope you're executing that it won't magically exist. The config context is short-term, during initialization only.
Rails.logger is the global where you can usually access it.
You can use the Rails logger outside of Rails models in at least version 2.3.X.
You might be used to doing the following in your models or controllers:
logger.info "Some debugging info I want to see in my development log."
If you make a regular model that doesn’t inherit from ActiveRecord, though, you may see the following error:
undefined local variable or method `logger' for #
The solution is to call Rails.logger.info (or debug, or warn) as follows:
Rails.logger.info "Some debugging info I want to see in my development log."
Error:
undefined method `author' for nil:NilClass
In my helper:
def last_updated(group)
g = group.last_updated_version
debugger
g.author.name
end
If I let my last_updated(group) function return group.last_updated_version, the view prints out my object as expected:
#<Assets::Version:0x0000000747af48>
And using the debugger at the point shown above, I can pull out the name
(rdb:1) g.author.name
"Administrator"
But returning group.last_updated_version.author.name results in the error.
Can anyone tell me why group.last_updated_version seems to return my object, but group.last_updated_version.author gives me the nil:NilClass error?
Sorry, silly problem. The helper last_updated() is used in a loop. My method last_updated_version returned nil for some of the group objects it was passed, but not the first. The debugger obviously stops on the first, so it worked in the debugger, but then broke on groups passed after that.