Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Tutorial - 2.5.2 Can't get validaiton exercise to work

I'm in the Rails Tutorial working on the exercises at the end of Chapter 2 and I'm stumped. www.railstutorial.org/book/toy_app#sec-toy_app_exercises

Assignment 2 says, "Update Listing 2.19 by replacing FILL_IN with the appropriate code to validate the presence of name and email attributes in the User model (Figure 2.20). "

And it's pretty straight forward Listing 2.19:

Adding presence validations to the User model. app/models/user.rb
class User < ActiveRecord::Base
  has_many :microposts
  validates FILL_IN, presence: true
  validates FILL_IN, presence: true
end

First thing I did was the typical noob mistake and just copied in the code straight out of the listing. System came back and asked me what this variable "FILL_IN" was.

Next thing i did, was to try putting in the field names in my user.rb file

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

Running this, gets me a the following error "NameError in UsersController#create" "undefined local variable or method `email' for #"

Rails is acting like it doesn't recognize the email, or name fields from my model.

I have tried capitalizing Name and Email, I have tried making them plurals, I have tried going to "rails console" to validate that I created the fields "name" and "email" correctly (I did).

I have tried looking for an answer to this, the closet I came was someone just pasting in the FILL_IN lines and getting harpooned for it.

I'm hoping that I haven't missed something just as obvious, but I'm prepared for it if I did.

like image 353
Malcolm Anderson Avatar asked Jan 19 '15 09:01

Malcolm Anderson


1 Answers

@Octopus-Paul Awesome, the colon(:) before the variable names is exactly what I needed.

class User < ActiveRecord::Base
    has_many :microposts
    validates :name, presence: true
    validates :email, presence: true
end
like image 72
Malcolm Anderson Avatar answered Nov 14 '22 22:11

Malcolm Anderson