ruby-on-rails – Rails教程 – 2.5.2无法获得有效的运动

我正在参与第2章末尾练习的Rails教程,我很难过.
www.railstutorial.org/book/toy_app#sec-toy_app_exercises

作业2说:“通过用适当的代码替换FILL_IN来更新清单2.19,以验证用户模型中是否存在名称和电子邮件属性(图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

我做的第一件事是典型的noob错误,只是直接从列表中复制代码.系统回来后问我这个变量“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
“UsersController中的NameError #create”
“未定义的局部变量或方法`email’for#”

Rails表现得像是无法识别电子邮件,或者从我的模型中命名字段.

我已经尝试过将名称和电子邮件大写,我试过让它们复数,我试图去“rails console”来验证我是否正确创建了“名称”和“电子邮件”字段(我做过).

我试过寻找答案,我来的衣柜是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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...