简体   繁体   中英

undefined method in Rails tutorial

I am following the rails tutorial by Michael Hartl and am running into a roadblock that doesnt seem to make sense.

I added a new method to the User class:

def full_name
  "test"
end

So when I do something like: test = User.new(name: 'Phil Collins', email: 't@gen.com') test.full_name

I get the error NoMethodError: undefined method `full_name'

Huh? It's totally in the User class. What am I missing?

Whenever Rails loads an application it will load all the classes in memory.

The Rails console invokes Rails.application.initialize! and will load the current code for the application.

Rails comes with nice features for developers to use while in development mode, notably spring.

https://github.com/rails/spring

However, this only works for development servers not the console.

There is a distinction, because when Rails loads a server then Rails::Server is defined. The console defines Rails::Console .

There is a way of reflecting code changes in console by using the reload! .

This will reflect code changes in the console, but there is a catch.

Old objects will not have changed, for this you must use test.reload .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM