ROR设计“电子邮件不能为空”错误

时间:2013-12-01 00:01:54

标签: ruby-on-rails devise

我正在按照教程设置预启动页面,使用Devise管理收集用户电子邮件。当我输入电子邮件地址时,我收到错误消息“1错误禁止此用户被保存:电子邮件不能为空” 我的用户模型:

class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
     :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me

after_create :add_user_to_mailchimp
before_destroy :remove_user_from_mailchimp

# override Devise method
# no password is required when the account is created; validate password   when the user sets one
validates_confirmation_of :password
def password_required?
if !persisted?
  !(password != "")
else
  !password.nil? || !password_confirmation.nil?
end
end

# override Devise method
def confirmation_required?
false
end

# override Devise method
def active_for_authentication?
confirmed? || confirmation_period_valid?
end

def send_reset_password_instructions
if self.confirmed?
  super
else
  errors.add :base, "You must receive an invitation before you set your password."
end
end

# new function to set the password
def attempt_set_password(params)
p = {}
p[:password] = params[:password]
p[:password_confirmation] = params[:password_confirmation]
update_attributes(p)
end

# new function to determine whether a password has been set
def has_no_password?
self.encrypted_password.blank?
end

# new function to provide access to protected method pending_any_confirmation
def only_if_unconfirmed
pending_any_confirmation {yield}
end

我的用户控制器:

before_filter :authenticate_user!

def index
authorize! :index, @user, :message => 'Not authorized as an administrator.'
@users = User.all
 end

def show
@user = User.find(params[:id])
end

def update
authorize! :update, @user, :message => 'Not authorized as an administrator.'
@user = User.find(params[:id])
if @user.update_attributes(params[:user], :as => :admin)
  redirect_to users_path, :notice => "User updated."
else
  redirect_to users_path, :alert => "Unable to update user."
end
end

def destroy
authorize! :destroy, @user, :message => 'Not authorized as an  administrator.'
user = User.find(params[:id])
unless user == current_user
  user.destroy
  redirect_to users_path, :notice => "User deleted."
else
  redirect_to users_path, :notice => "Can't delete yourself."
end
end

设计/注册/新视图

<div class="email-capture">
<%=form_for(resource, :as => resource_name, :url =>  registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.text_field :email, :class=>"email"%>
<%= f.submit " ", :class=>"reg-btn"%>
<% end %>
</div>

点击提交后记录消息

Started POST "/users" for 127.0.0.1 at 2013-12-02 10:36:11 -0500
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"√", "authenticity_token"=>"mytoken", "user"=> {"email"=>"test@email.com"},
"commit"=>" "}
←[1m←[36m (0.0ms)←[0m  ←[1mbegin transaction←[0m
←[1m←[35m (0.0ms)←[0m  rollback transaction
Rendered devise/registrations/new.html.erb within layouts/application (5.0ms)
Rendered layouts/_messages.html.erb (0.0ms)

2 个答案:

答案 0 :(得分:2)

您是否修改了注册表单?

确保电子邮件输入有name="user[email]"

当您提交注册表单时,您在params的服务器日志中看到了什么?

此外,显示的users_controller应仅供管理员使用 - 请确保您不会意外地将注册路由到该管理员。

更新:

这是一个疯狂的猜测,但我注意到你正在进行数据库回滚。您是否可能有数据库限制要求密码不为NULL,例如?

答案 1 :(得分:0)

看看(https://github.com/plataformatec/devise#strong-parameters),可能是强参数问题,尝试在application_controller.rb中添加如下行:

  before_filter :configure_permitted_parameters, if: :devise_controller?
  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:name, :email) }
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password, :password_confirmation) }
  end