2010-05-23 53 views
2

我根据AuthLogic示例设置了AuthLogic for Rails:http://github.com/binarylogic/authlogic_example一个AuthLogic表单给我错误的验证错误 - 为什么?

我可以成功登录到系统,但用户访问时/ 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 [email protected] 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 
+0

您可以添加用于创建操作和用户模型代码的代码吗?尝试通过红宝石控制台创建用户,看看你是否得到任何错误。还有一个 – Vamsi 2010-05-23 05:10:24

回答

5

检查PARAMS [:用户],看看它是否发送正确的价值观

您还可以删除在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

只是一个想法,你有没有质量分配中较高比你的模型类禁用?

我已经收到凡在我的初始化属性我残疾质量分配,然后忘了把我的模型的属性,如访问使用模型类中的所有必需的属性attr_accessible这一点。

1

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

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

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