Rails 教程 - 2.5.2 无法进行验证练习

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

我正在 Rails 教程中进行第 2 章末尾的练习,但我被难住了。 www.railstutorial.org/book/toy_app#sec-toy_app_exercises

作业 2 说,"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). "

而且非常简单 清单 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

我做的第一件事是典型的菜鸟错误,直接从列表中复制代码。系统回来问我这个变量"FILL_IN"是什么

接下来我做的是尝试在我的 user.rb 文件中输入字段名称

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 表现得好像无法识别我的模型中的电子邮件或名称字段。

我试过将 Name 和 Email 大写,我试过将它们变成复数形式,我试过去 "rails console" 验证我是否正确创建了 "name" 和 "email" 字段(我做到了)。

我试过寻找这个问题的答案,我来的壁橱是someone just pasting in the FILL_IN lines and getting harpooned for it.

我希望我没有错过同样明显的东西,但如果我错过了,我已经做好了准备。

@章鱼保罗 太棒了,变量名前的冒号(:)正是我需要的。

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