我的设置 - Rails 4,ActiveAdmin,设计生成的用户模型。用户使用用户名进行身份验证,并且没有电子邮件属性。我要提到最后一个,因为设计严重依赖电子邮件属性,所以这可能与问题有关。我的确切设置和代码在此博客文章中进行了描述。
在 ActiveAdmin 后端,我转到用户 -> 新用户 -> 填写用户名、密码、密码确认 -> 创建用户。新用户表单没有创建新用户,而是被清除了,并且在密码字段下我得到了错误can't be blank
。当我转到 Rails 控制台并手动创建一个新用户时,User.create(username: 'Joe', password: 'password', password_confirmation: 'password')
一切正常,用户可以在localhost:3000/users/sign_in
.
我看到了这个问题。如果我添加到我的用户模型:
def password_required?
new_record? ? false : super
end
我可以创建一个新用户,但所有字段(包括用户名、加密密码)都是空白的。
更新正如 Leger 所建议的,我正在发布我的代码。由于我使用的是 Devise 和 Activeadmin 的内置控制器,我认为只有在 Activeadmin 和我的数据库模式中发布我的用户资源的代码才有意义:
Activeadmin 中的用户资源:
ActiveAdmin.register User do
# This determines which attributes of the User model will be displayed in the index page. I have left only username, but feel free to uncomment the rest of the lines or add any other of the User attributes.
index do
column :username
# column :current_sign_in_at
# column :last_sign_in_at
# column :sign_in_count
default_actions
end
# Default is :email, but we need to replace this with :username
filter :username
# This is the form for creating a new user using the Admin backend. If you have added additional attributes to the User model, you need to include them here.
form do |f|
f.inputs "User Details" do
f.input :username
f.input :password
f.input :password_confirmation
end
f.actions
end
# This is related to Rails 4 and the changes it introduced in handling strong parameters. Here we replace :email with :username.
controller do
def permitted_params
params.permit admin_user: [:username, :password, :password_confirmation]
end
end
end
架构.rb:
ctiveRecord::Schema.define(version: 20131031102826) do
create_table "active_admin_comments", force: true do |t|
t.string "namespace"
t.text "body"
t.string "resource_id", null: false
t.string "resource_type", null: false
t.integer "author_id"
t.string "author_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace"
add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
create_table "admin_users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
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"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true
add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true
create_table "users", force: true do |t|
t.string "username", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
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"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
add_index "users", ["username"], name: "index_users_on_username", unique: true
end
此处提供任何其他代码。