尝试在Rails 4中通过ActiveAdmin创建新用户时,密码“不能为空”

时间:2013-11-01 08:56:10

标签: ruby-on-rails devise activeadmin

我的设置 - Rails 4,ActiveAdmin,Devise生成的用户模型。用户使用用户名进行身份验证,但没有email属性。我提到了最后一个,因为Devise严重依赖于email属性,所以这可能与问题有关。我的确切设置和代码描述为in this blog post

在ActiveAdmin后端,我转到用户 - >新用户 - >填写用户名,密码,密码确认 - >创建用户。新用户表单被删除而不是创建新用户,而在密码字段下,我收到错误can't be blank。当我转到Rails控制台并手动创建新用户时User.create(username: 'Joe', password: 'password', password_confirmation: 'password')一切正常,用户可以在localhost:3000/users/sign_in登录。

我看到了SO question。如果我添加到我的用户模型:

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

schema.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

其他任何代码都可用here

1 个答案:

答案 0 :(得分:5)

我认为问题在于处理permitted_params模型的ActiveAdmin页面中的User方法:

params.permit admin_user: [:username, :password, :password_confirmation]

如果这实际上是User模型,则该行应为:

params.permit user: [:username, :password, :password_confirmation]

这符合您所描述的症状 - 提交后的空表单以及控制台中的所有内容都正常工作。