记录:验证两个值是否输入一致

今天在复写devise的修改密码和修改email的功能,突然发现自己并不知道devise是如何做到。确认两次输入密码是否一致的。 在网上找到了一个叫validates_confirmation_of的方法. 下面是用rails101来做实验,要求两次title输入一致 group.rb

validates_presence_of :title
validates_confirmation_of :title

发现这样写并没有出现效果。查看server log

Started POST "/groups" for ::1 at 2017-02-14 18:46:01 +0800
Processing by GroupsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"QjuJLjbUSiKHWxhxYQ3+CeD8dnCxmIFqG9a6/8eGKJbtCOyi/jgcjS/NSA6OMH4eFLxlR9+Lie0ILBHzJyanoQ==", "group"=>{"title"=>"3213123", "title_confirmation"=>"3123213", "description"=>"12312313"}, "commit"=>"Submit"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 3], ["LIMIT", 1]]
Unpermitted parameter: title_confirmation
   (0.0ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "groups" ("title", "description", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["title", "3213123"], ["description", "12312313"], ["created_at", 2017-02-14 10:46:02 UTC], ["updated_at", 2017-02-14 10:46:02 UTC], ["user_id", 3]]
   (0.5ms)  commit transaction
Redirected to http://localhost:3000/groups
Completed 302 Found in 6ms (ActiveRecord: 1.0ms)

发现title_confirmation并没有被update。需要在strong_params里面加入。

 def group_params
   params.require(:group).permit(:title, :description,:title_confirmation)
 end

error API