When I try to create a new user using Activeadmin, Rails throws the following error:
NoMethodError in Admin::UsersController#create
undefined method `email' for #<User:0x007fabb13db508>
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"RS+AP9merlR5RxiKrPxM8Sx9CYEZGn7a4EJ6e25DL6Q=",
"user"=>{"username"=>"user1",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Create User"}
Some background info: I have installed Activeadmin and besided the default admin_user resource, I created a user model using Devise and registered this user model as an Activeadmin resource. I want my users to authenticate using usernames and not emails, so I did the following migration:
def up
add_column :users, :username, :string
add_index :users, :username, unique: true, name: 'index_users_on_username', using: 'btree'
remove_column :users, :email, :string
end
def down
remove_column :users, :username, :string
remove_index :users, :username
add_column :users, :email, :string
end
In the devise initializer I set:
config.authentication_keys = [ :username ]
config.case_insensitive_keys = [ :username ]
config.strip_whitespace_keys = [ :username ]
I also did the appropriate customizations of the login forms and the forms of the user resource in Activeadmin:
ActiveAdmin.register User do
index do
column :username
default_actions
end
filter :username
form do |f|
f.inputs "Admin Details" do
f.input :username
f.input :password
f.input :password_confirmation
end
f.actions
end
controller do
def permitted_params
params.permit admin_user: [:username, :password, :password_confirmation]
end
end
end
I cannot determine if this is a Devise, Activeadmin, Postgres or Rails issue. Furthermore, I am not sure where to look for Admin::UsersController. Any help would be appreciated.
P.S. In the Rails console tring to create a new user:
User.create(username: 'John', password: '123456', password_confirmation: '123456')
throws a similar error:
NoMethodError: undefined method `email' for #<User:0x007fabb6806da8>
Update 1:
@Sparda: I added this to my user model (under app/models/user.rb):
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if username = conditions.delete(:username)
where(conditions).where(["lower(username) = :value", { :value => username.downcase }]).first
else
where(conditions).first
end
end
restarted the server, but the same error is there.
@Adeptus: I added the two methods in add/admin/user.rb. Now the following happens - when I fill the form and click on Create user the page refreshes, the field of the forms get erased and only there is a flash error arround the password field can't be blank. Btw, I had already encountered this behaviour. I tried to readd the email column in the users table, but that same thing happend.
Usermodel and let me know what happens, if this works I'll write a complete answerdef self.find_first_by_auth_conditions(warden_conditions) conditions = warden_conditions.dup if username = conditions.delete(:username) where(conditions).where(["lower(username) = :value", { :value => username.downcase }]).first else where(conditions).first end end- Raindalwherecan still be used as it was before. If you are using MetaWhere at other places though, you actually might want to replace it for Squeel. Otherwise you don'y need neither one or the other. - Raindal