1

我正在尝试将新用户添加到现有数据库。每当我为此使用注册表单时,它都会导致查找电子邮件为 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

用户控制器.rb

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

2 回答 2

0

我实际上发现了错误:

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

参数更改为 :user 并完成。

于 2012-07-25T00:01:42.823 回答
0

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

于 2012-07-24T22:19:20.573 回答