我正在按照教程使用 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
我的用户 Controller :
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
设计/注册/新 View
<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"=>"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b3f2e383f0b2e262a222765282426" rel="noreferrer noopener nofollow">[email protected]</a>"},
"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)
最佳答案
您修改过注册表吗?
确保电子邮件输入具有name="user[email]"
当您提交注册表单时,您会在服务器日志中看到 params
的哪些内容?
此外,显示的 users_controller 应该仅供管理员使用 - 确保您不会意外地将注册路由到它。
更新:
这是一个疯狂的猜测,但我注意到您正在得到数据库回滚。例如,您是否可能有一个数据库限制,要求密码不能为 NULL?
关于ruby-on-rails - ROR Devise "Email can' t 为空白"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20307149/