问题
I just added active admin to my rails app and I'm unable to create a new user. I am using the user model that active admin creates with a few added columns like first and last name. When I fill out the form for a new user and click create new user, that page refreshes but doesn't save my user and doesn't go to the recap page with the successful message.
here is my AdminUser model
class AdminUser < ActiveRecord::Base
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name
end
And here is my active admin class
ActiveAdmin.register AdminUser do
index do
column :first_name
column :last_name
column :email
default_actions
end
form do |f|
f.inputs "User Details" do
f.inputs :email
f.inputs :first_name
f.inputs :last_name
end
f.buttons
end
end
回答1:
My solution:
ActiveAdmin.register User do
permit_params [:email, :password, :password_confirmation]
form do |f|
f.inputs "User" do
f.input :email
f.input :password
f.input :password_confirmation
end
f.actions
end
end
回答2:
Forgot to add this little guy to the model...fml
after_create { |admin| admin.send_reset_password_instructions }
def password_required?
new_record? ? false : super
end
回答3:
This is to do with a code reloading bug in Rails which is manifested when your environment specifies config.cache_classes = false
.
Change that to true in your config/environments/development.rb
, restart your server, and you should be able to create your user.
However that's not ideal, and one workaround suggested here is to put the following in your config/environments/development.rb
:
config.to_prepare do
Thread.current.keys.each{ |k| Thread.current[k] = nil if k.to_s =~ /_scoped_methods$/ }
end
Although the bug appears to be resolved, I see the issue in 3.1.1 which the above code fixes.
Even though this is a bug in Rails, it's also logged as a bug in active_admin if you want to see more discussion on this.
回答4:
Upvoting @Danpe's answer. "Password" is a required field. So you need to add it to permit_params and also ask for password in the form. Only then will it save the form correctly. Here is my permit params string that also fixes other issues with creating a ActiveAdmin user mentioned here : https://github.com/gregbell/active_admin/issues/2595
controller do
def permitted_params
params.permit :utf8, :_method, :authenticity_token, :commit, :id,
model: [:attribute1, :attribute2, etc]
end
end
来源:https://stackoverflow.com/questions/8126747/cant-create-new-user-with-activeadmin