Emily Chiu Emily Chiu - 7 months ago 33
Ruby Question

NameError in UsersController#index uninitialized constant User::FILL_IN

I am new to Rails and currently following Michael Hartl's Ruby on Rails Tutorial. When I try adding these last lines in app/models/user.rb at the end of Chapter 2: A toy app, I run into this error:

(Error)


NameError in UsersController#index
uninitialized constant User::FILL_IN


(Code)

class User < ActiveRecord::Base
has_many :microposts
validates FILL_IN, presence: true
validates FILL_IN, presence: true
end


Sorry if this might be a stupid error of typo, or syntax but can someone please help me understand what I've done wrong?

Thanks

Answer

FILL_IN is a placeholder.

Replace FILL_IN with the name of the column you want validated. Example below:

validates :title, presence: true
validates :description, presence: true

The above code, inside a model, will ensure that both the title and description columns (fields) are filled and not nil before saving the new record, ( or an updated record ).

PS: this is example, assuming the presence of title and description fields in the model. these would have to be replaced with the name of the columns of interest which you want validated for presence (which will be name and email in this case).

Hope this is explanatory enough and is helpful.

Update:

Look at the exercise here. This point is made more obvious. :)