开发者

RoR: why does this validation fail?

This one really has me. I have this validation in my user model:

validates :first_class, :presence => true, :inclusion => %w(Fighter Ranger Magician)

Now, I try an example create in my console:

开发者_JAVA百科
ruby-1.9.2-p180 :053 > new = User.create(:first_class => 'Magician')
 => #<User id: nil, ...
ruby-1.9.2-p180 :054 > new.errors
 => {:first_class=>["can't be blank", "is not included in the list"]}

Why am I getting this validation error? I SERIOUSLY cannot figure that out.

(If i remove the validation, the user gets created, but first_class is nil :O)


maybe try having attr_accessible :first_class in your model file

You have to tell rails which attributes are writeable through mass-assignment. The new method takes a parameters hash, which is considered mass-assignment. The same is true with update_attributes.

To verify, you could just make a new instance and say object.first_class = 'Magician'. If this also fails, then you know attr_accessible is not the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜