AuthLogic表单给出了错误的验证错误 - 为什么?

时间:2010-05-23 04:11:22

标签: ruby-on-rails validation authlogic

我根据AuthLogic示例设置了AuthLogic for Rails:http://github.com/binarylogic/authlogic_example

我可以成功登录系统,但是当访问users / new.html.erb注册新用户时,表单会返回以下验证错误:

Email is too short (minimum is 6 characters)
Email should look like an email address.
Login is too short (minimum is 3 characters)
Login should use only letters, numbers, spaces, and .-_@ please.
Password is too short (minimum is 4 characters)
Password confirmation is too short (minimum is 4 characters)

我输入的数据中不存在这些错误。

# new.html.erb
<%= form.label :login, nil, :class => "label" %><br />
<%= form.text_field :login, :class => "inputBox",
    :name => "login", :type => "text" %><br />

<%= form.label :password, form.object.new_record? ? nil : "Change password", :class => "label"  %><br />
<%= form.password_field :password, :class => "inputBox", 
    :name => "password", :type => "text" %><br />

<%= form.label "Confirm password", nil, :class => "label" %><br />
<%= form.password_field :password_confirmation, :class => "inputBox",
    :name => "password_confirmation", :type => "text" %><br />

<%= form.label :email, nil, :class => "label" %><br />
<%= form.text_field :email, :class => "inputBox",
    :name => "email", :type => "text" %><br />

# Users controller
def new
  @user = User.new
  render :layout => "forms"
end

我认为问题是数据没有以某种方式传输,因此AuthLogic认为输入不够。您是否知道为什么AuthLogic告诉我数据不满足其验证?

------更多信息------

# User model
class User < ActiveRecord::Base
  acts_as_authentic
  belongs_to :authenticable, :polymorphic => true
end

# Users controller, def create:
def create
    @user = User.new(params[:user])
  if @user.save
    flash[:notice] = "Account registered!"
    redirect_back_or_default account_url
  else
    render :action => :new
  end
end

3 个答案:

答案 0 :(得分:5)

检查params [:user]并查看它是否正在发送正确的值

您还可以在模型中删除acts_as_authentic添加以下字段的验证:

acts_as_authentic do |c|
 c.validate_login_field = false
 c.validate_email_field = false
 c.validate_password_field = false
end

答案 1 :(得分:1)

只是一个想法,你是否在比模型类更高的位置禁用了质量分配?

在我在初始值设定项中禁用了属性的质量分配之前,我已经发生了这种情况,然后忘记使用 attr_accessible 将模型的属性设置为模型类中所有必需属性。

答案 2 :(得分:1)

文档和视频中不明确的一点是:即使您在模型中有crypted_pa​​ssword(即attr_accessible:password),attr_accessible行仍然需要密码。

我不确定,但我假设authlogic在加密之前使用此密码变量来保存明文版本。

我不建议设置c.validate_password_field = false,因为这也会关闭加密。