发送错误的查询,电子邮件IS NULL

时间:2012-07-24 22:04:05

标签: ruby-on-rails activerecord

我正在尝试将新用户添加到现有数据库中。每当我使用注册表单时,它会导致查找电子邮件为NULL的用户。

(0.1ms)  begin transaction
User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
(0.1ms)  rollback transaction

查看服务器日志,它实际上说明了有效的电子邮件地址:

Parameters: {"utf8"=>"✓",
"authenticity_token"=>"epYmQoIfibeimZEFYo5MdSIqjJM3sLZgTtj79rMpvyQ=", "user"=>
 {"name"=>"Herp Derp", "email"=>"lang.martin@herpderp.de", "password"=>"[FILTERED]",      
 "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}

user.rb如下所示:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }


  validates :name, presence: true,
                   length: {maximum: 40}

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true,
          format: { with: VALID_EMAIL_REGEX },
          uniqueness: { case_sensitive: false }

  validates :password, length: {minimum: 6}
  validates :password_confirmation, presence: true
end

以下表格:(使用Formtastic)

%h1 Sign Up
%div.row
  %div.span6.offset3
    = semantic_form_for @user do |f|
      = f.inputs do
        = f.input :name
        = f.input :email
        = f.input :password
        = f.input :password_confirmation
        = f.action :submit

user_controller.rb

  def create
    @user = User.new(params[:users])
    if @user.save
      redirect_to @user
    else
      render 'new'
    end
  end

2 个答案:

答案 0 :(得分:0)

您是否尝试在rails控制台(rails s)中创建新用户?

答案 1 :(得分:0)

我实际上发现了错误:

def create
   @user = User.new(params[:users])
  if @user.save
  redirect_to @user
else
  render 'new'
end

params改为:user and done。