【问题标题】:An AuthLogic form is giving me incorrect validation errors -- why?AuthLogic 表单给了我不正确的验证错误——为什么?
【发布时间】:2010-05-23 04:11:21
【问题描述】:

我根据 AuthLogic 示例为 Rails 设置 AuthLogic: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

【问题讨论】:

  • 能否请您添加创建操作的代码以及您的用户模型代码?尝试通过 ruby​​ 控制台创建用户,看看是否有任何错误。还有一个

标签: ruby-on-rails validation authlogic


【解决方案1】:

检查 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

【讨论】:

    【解决方案2】:

    只是想一想,你是否在高于你的模型类的地方禁用了批量分配?

    在我禁用初始化器中的属性的批量分配之前,我曾发生过这种情况,然后忘记使用 attr_accessible 将模型的属性设置为可访问模型类中的所有必需属性。

    【讨论】:

      【解决方案3】:

      文档和视频中不清楚的一点是 attr_accessible 行中仍然需要 :password ,即使模型中可能有 crypted_pa​​ssword (即 attr_accessible :password)。

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

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

      【讨论】:

        猜你喜欢
        • 2015-02-19
        • 1970-01-01
        • 1970-01-01
        • 2017-06-18
        • 1970-01-01
        • 2022-01-07
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        相关资源
        最近更新 更多