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.
Related
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.
So I have an if statement. If a customer exists return false. I was trying with an actual customer id but it was throwing an error, I replaced it with a 1. Still getting that error.
if Stripe::Customer.retrieve(1)
throws
undefined method `encoding' for 1:Fixnum
link to the api :
https://stripe.com/docs/api#retrieve_customer
All I had to do was
require "stripe"
... :|
Please check the https://stripe.com/docs/api/ruby#customers
There you can find that, in attributes it takes
id: string
So the code should be
#Stripe::Customer.retrieve({CUSTOMER_ID})
Stripe::Customer.retrieve(1.to_s)
Hope it will work.
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'm just getting the hang of Rails console, and finding it useful for quickly testing methods in my classes. I know that I can make changes to my Models, then
> reload!
to grab those updates, but sometimes I'll find that it doesn't seem to reload my latest code. Does Rails cache code somewhere?
In a really simple pseudo example, I may have bad code on line 100:
100: u = User.alll
and in the Rails console, when I run this method, I might get an error similar to:
NoMethodError: undefined method `alll' for User:Class ... on line 100
then modify my code, fixing the error
100: u = User.all
then reload:
> reload!
and then, when calling the method in this class that has the correct code, it still will say
NoMethodError: undefined method `alll' for User:Class ... on line 100
When clearly, the error is fixed, and the offending line isn't even on line 100 anymore. Is there a way to force/hard-reset the "reload!" command?
My guess would be that you're doing something like:
Create an instance of User
Call someMethod on the instance
You get an error, and you go and fix it
reload!
You call someMethod on the existing instance and get the error again
So you're calling the method on an instance that hasn't itself been reloaded. Its class has been reloaded, but the instance is already in memory - with bugs and all.
That would be my guess at least (not 100% sure).
Point is, if you create a new instance after the reload! and call your method on that new instance, it should stop complaining.
I am using the Cucumber unit test tool, and am trying to retrieve the value of a textarea, however I can't get it to work.
This is my code...
page.find(:xpath, "//div[#id='process']/table/tbody/tr/td/div/textarea").value
This is the error I'm getting...
Error: undefined method `value' for nil:NilClass (NoMethodError)
This URL listed below confirms that value is the correct method to retrieve the value:
http://www.w3schools.com/jsref/dom_obj_textarea.asp
Could someone please help me fix this problem.
The nil error indicates that you're not getting the textarea -- find is returning nil. Try a simpler xpath query, or referencing the textarea by name or id.