簡體   English   中英

在Rails 4中嘗試通過ActiveAdmin創建新用戶時密碼“不能為空”

[英]Password “can't be blank” when trying to create a new user via ActiveAdmin in Rails 4

我的設置-Rails 4,ActiveAdmin,Devise生成的用戶模型。 用戶使用用戶名進行身份驗證,並且沒有電子郵件屬性。 我要說的是最后一個,因為Devise嚴重依賴於email屬性,因此這可能與問題有關。 本博客文章中介紹我的確切設置和代碼。

在ActiveAdmin后端,我轉到用戶->新用戶->填寫用戶名,密碼,密碼確認->創建用戶。 沒有創建新用戶,而是清除了“新用戶”表單,並且在“密碼”字段下得到的錯誤can't be blank 當我進入Rails控制台並手動創建一個新用戶時, User.create(username: 'Joe', password: 'password', password_confirmation: 'password')一切正常,並且用戶能夠在localhost:3000/users/sign_in登錄。 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

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

其他任何代碼都可以在這里找到

我認為問題出在permitted_params在ActiveAdmin頁面處理方法, User模型:

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

如果這實際上是User模型,則該行應顯示為:

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

這符合您所描述的症狀-提交后為空表格,並且所有內容均可在控制台中正常運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM