论坛首页 Ruby版 rails

validates_ 中碰到的问题求教

浏览 467 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-01-11
在学习rails的过程中碰到一个问题:validates_confirmation_of :password :on => :create不生效,请教各位。 rails 1.2.6.

class Member < ActiveRecord::Base
  attr_accessor :password_confirmation
  belongs_to :current_addr, :class_name => 'Location', :foreign_key => "address"
  belongs_to :hometown_addr, :class_name => 'Location', :foreign_key => "hometown"
  validates_confirmation_of :password, :message => '密码不一致', :on => :create
  validates_presence_of :email, :password
  validates_uniqueness_of :email
end

class MemberController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  def new
    if request.get?
      @member = Member.new
    else
      @member = Member.new(params[:member])
      if @member.create
        redirect_to(:action => "index")
      end
    end
  end
  def destroy
    Member.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end


而validates_confirmation_of :password,采用@member.save却有效,不懂。
   
最后更新时间:2008-01-11
请自己看rails文档
注意黑体字部分
引用

doc/api/classes/ActiveRecord/Validations/ClassMethods.html#M001332
validates_confirmation_of(*attr_names)

Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:
  Model:
    class Person < ActiveRecord::Base
      validates_confirmation_of :user_name, :password
      validates_confirmation_of :email_address, :message => "should match confirmation"
    end

  View:
    <%= password_field "person", "password" %>
    <%= password_field "person", "password_confirmation" %>

The added password_confirmation attribute is virtual; it exists only as an in-memory attribute for validating the password. To achieve this, the validation adds acccessors to the model for the confirmation attribute. NOTE: This check is performed only if password_confirmation is not nil, and by default only on save. To require confirmation, make sure to add a presence check for the confirmation attribute:
  validates_presence_of :password_confirmation, :if => :password_changed?

Configuration options:

    * message - A custom error message (default is: "doesn‘t match confirmation")
    * on - Specifies when this validation is active (default is :save, other options :create, :update)
    * if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.
    * unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.
   
0 请登录后投票
最后更新时间:2008-01-11
谢谢。
没有仔细看文档。
   
0 请登录后投票
论坛首页 Ruby版 rails

跳转论坛:
JavaEye推荐