我有一个rails(6.0.2.2)和devise(4.7.1)的项目,有两种账户:教师和学生。学生可以成功注册,但老师的账号却因为某些原因出现了问题。当我尝试建立一个测试账户时,我得到的是
Unpermitted parameter: :email
错误信息,我的信息没有保存在数据库中。
迁移。
class DeviseCreateTeachers < ActiveRecord::Migration[6.0]
def change
create_table :teachers do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
t.string :username
t.string :name
t.string :neighborhood
t.integer :pet_skin
t.integer :pet_eyes
t.integer :pet_face
t.integer :pet_accessories
## Trackable
# t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both
t.datetime :locked_at
t.timestamps null: false
end
add_index :teachers, :email, unique: true
add_index :teachers, :reset_password_token, unique: true
# add_index :teachers, :confirmation_token, unique: true
# add_index :teachers, :unlock_token, unique: true
end
end
我的 teacher_controller.rb:
class TeacherController < ApplicationController
private
def sign_up_params
params.require(:teacher).permit(:email, :username, :name, :password, :password_confirmation, :neighborhood, :pet_skin, :pet_eyes, :pet_face)
end
def account_update_params
params.require(:teacher).permit(:username, :password, :password_confirmation, :current_password)
end
end
我使用的是默认生成的teacherregistrationnew.html.erb,所以我认为这里没有问题。
我在configinitializersdevise.rb中添加了一行。
config.authentication_keys = [:username]
将默认的登录方式从电子邮件改为用户名,但我认为这不是问题,因为学生也可以创建账户。
谢谢你的帮助!我使用的是rails(6.0)。
@Tijana 我不知道你是否已经检查了这个链接。如果没有,它可能会有帮助https:/github.comheartcombodevise#strong-parameters。