Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Password "can't be blank" when trying to create a new user via ActiveAdmin in Rails 4

My setup - Rails 4, ActiveAdmin, Devise generated User model. The users authenticate with usernames and do not have the email attribute. I am mentioning the last, as Devise heavily relies on the email attribute, so this might have to do with the problem. My exact setup and code are described in this blog post.

In ActiveAdmin back-end, I go to Users -> New User -> fill in username, password, password confirmation -> Create User. Instead of creating a new user the New User form gets wiped out and under the Password field I get the error can't be blank. When I go to the Rails console and create a new user manually User.create(username: 'Joe', password: 'password', password_confirmation: 'password') everything works and the user is able to log in at localhost:3000/users/sign_in.

I saw this SO question. If I add to my User model:

def password_required?
  new_record? ? false : super
end

I can create a new user, but all of the fields (including username, encrypted password) are blank.

UPDATE As Leger suggest, I am posting my code. As I am using the built-in contrllers of Devise and Activeadmin, I think it only makes sense to post the code of my User resource in Activeadmin and my db schema:

User resource in 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

Any other code is available here.

like image 961
Alexander Popov Avatar asked Nov 01 '13 08:11

Alexander Popov


1 Answers

I think the problem lies in the permitted_params method in your ActiveAdmin page handling the User model:

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

If this is actually the User model, the line should read:

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

That fits the simptoms you describe–empty form after submit and everything working properly in the console.

like image 84
Dimitar Avatar answered Nov 03 '22 04:11

Dimitar