我正在使用clearance gem,它基本上是通过电子邮件进行的身份验证。
现在我想通过电子邮件保持登录,但想在用户的注册表单中添加一个“名称”字段。
用户.rb
class User < ActiveRecord::Base
include Clearance::User
validates_presence_of :name
validates_uniqueness_of :name
end
users_controller.rb
class UsersController < Clearance::UsersController
def create
@user = user_from_params
if @user.save(permit_params)
sign_in @user
render :json => {:success => true}
else
render :json => {:success => false}
end
end
private
def user_from_params
user_params = params[:user] || Hash.new
email = user_params.delete(:email)
password = user_params.delete(:password)
Clearance.configuration.user_model.new(user_params).tap do |user|
user.email = email
user.password = password
end
end
def permit_params
params.require(:user).permit(:name, :email, :encrypted_password, :password, :confirmation_token, :remember_token)
end
end
如您所见,我添加了权限.save
,但应用程序仍应请求向我抛出以下错误:
ActiveModel::ForbiddenAttributesError in Clearance::UsersController#create
如果我:name
从表单中删除文本字段,一切正常,但我想要表单的其他字段。