无法绕过活动记录中的验证错误'You need to supply at least one attribute'?



我一直在遵循Michael Hartl教程,但采取了快捷方式试图"改进"其示例,并创建了一个棘手的错误…

基本上,这个应用程序围绕着一个用户模型,加上一个注册功能,使用了大量围绕加密密码功能的验证;对于rails新手来说,这可不是什么小事。

我没有严格遵循Hartl的示例,而是修改了使用scaffold来设置用户的方法。但是在他的项目结束的时候,我开始得到这个错误,当试图创建一个用户:

ArgumentError in UsersController#new
You need to supply at least one attribute.

这让我很困惑,因为我已经尝试关闭所有验证,除了保存。下面是一些代码片段:

我users_controller.rb #新:

class UsersController < ApplicationController
  # GET /users
  # GET /users.xml
  def index
    @users = User.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end
.....

  def new
    @title = "Sign up"
    @user = User.new
    #(unnecessary?)
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end

from my user.rb:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessor :username, :firstname, :lastname, :email, :password, :password_confirmation
  email_regex = /A[w+-.]+@[a-zd-.]+.[a-z]+z/i
  # here I am attempting to only validate when saving
  validates :allow_nil => true, :presence => true, :on => :save
  validates :firstname,  :presence => true,
                    :length   => { :maximum => 50 }
  validates :lastname,  :presence => true,
                    :length   => { :maximum => 50 }
  validates :username,  :presence => true,
                    :length   => { :maximum => 20 }
  validates :email, :presence => false,
                    :format   => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }
  # Automatically create the virtual attribute 'password_confirmation'.
  validates :password, :presence     => true,
                       :confirmation => true,
                       :length       => { :within => 6..40 }
  before_save :encrypt_password
   # Return true if the user's password matches the submitted password.
  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)
  end
  def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil  if user.nil?
    return user if user.has_password?(submitted_password)
  end
  def self.authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt) ? user : nil
  end
  def initialize(attributes = {})
    @username  = attributes[:username]
    @firstname  = attributes[:firstname]
    @lastname  = attributes[:lastname]
    @email = attributes[:email]
  end
  def formatted_email
    "#{@firstname} #{@lastname}<#{@email}>"
  end
  private
    def encrypt_password
      self.salt = make_salt unless has_password?(password)
      self.encrypted_password = encrypt(password)
    end
    def encrypt(string)
      secure_hash("#{salt}--#{string}")
    end
    def make_salt
      secure_hash("#{Time.now.utc}--#{password}")
    end
    def secure_hash(string)
      Digest::SHA2.hexdigest(string)
    end
end

任何建议都非常感谢!

——里克

PS——每个请求。下面是create方法:

def create
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.save
        format.html { redirect_to(@user, :notice => 'User was successfully created.') }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

我能够通过注释掉我模型中的所有验证来获得错误消息,但是我将一个接一个地添加它们;所以我仍然需要解决这个bug....

User模型中的这一行:

validates :allow_nil => true, :presence => true, :on => :save

未提供用于验证的属性。您可能想要在下面列出的每个验证中包含这些选项,但validates不是这样工作的。

我同意jimworm的观点。验证不运行在新的。它仅在您尝试保存或调用valid?之前运行。你只需要去掉jimworm的轮廓线。

相关内容

最新更新