ruby-on-rails – Ruby NoMethodError

Okay, I’m a little novice. I know this error is happening because I don’t understand how to call the method correctly. So can you help me understand what’s going wrong here?

NoMethodError in ThingController #index
Undefined method `has been initialized? ‘for Thing :: Backend: Class

From the wrong part of ThingController.rb:

class ThingController
def init_things
Backend .init_things unless Backend.initialized?
end

t = ThingController.new
t.init_things
end

In Backend.rb< /p>

class Backend
# checks if the things hash is initialized
def initialized?
@initialized ||= false
end< br />
# loads things
def init_things
puts "I've loaded a bunch of files into a hash"
@initialized = true
end
end

I didn’t call the method correctly, and I couldn’t find any clear explanation about this error on the Internet. Please help.

Thank you

It seems that the problem is that the initialization method you declare in Backend is an instance method. When you then call Backend.initialized? Are you calling the initialized class method? For the backend class. This method is undefined, so NoMethodError will be raised. Can you do this by using def self.initialized? Declare a method to solve this problem. If you really want this to be a class method, you may need to consider how to organize the rest of the code.

You can find it at http://railstips.org/blog/ archives/2009/05/11/class-and-instance-methods-in-ruby/ find more information about classes and instance methods

Ok, I’m a little bit Newbie. I know this error is happening because I don’t understand how to call the method correctly. So can you help me understand what is going wrong here?

NoMethodError in ThingController #index
Undefined method `has been initialized? ‘for Thing :: Backend: Class

From the wrong part of ThingController.rb:

class ThingController
def init_things
Backend .init_things unless Backend.initialized?
end

t = ThingController.new
t.init_things
end

In Backend.rb< /p>

class Backend
# checks if the things hash is initialized
def initialized?
@initialized ||= false
end< br />
# loads things
def init_things
puts "I've loaded a bunch of files into a hash"
@initialized = true
end
end

I didn’t call the method correctly, and I couldn’t find any clear explanation about this error on the Internet. Please help.

Thank you

It seems that the problem is that the initialization method you declared in Backend is an instance method. When you then call Backend.initialized? Are you calling the initialized class method? For the backend class. This method is undefined, so NoMethodError will be raised. Can you do this by using def self.initialized? Declare a method to solve this problem. If you really want this to be a class method, you may need to consider how to organize the rest of the code.

You can find it at http://railstips.org/blog/ archives/2009/05/11/class-and-instance-methods-in-ruby/Find more information about classes and instance methods

Leave a Comment

Your email address will not be published.