我正在尝试在activeadmin中创建一个页面,用户可以进入该页面并创建新的用户帐户。
我正在使用下面的代码覆盖我的用户模型的默认创建方法。
当我尝试渲染Couldn't find User without an ID
页面时,我收到错误new
。
尝试重新渲染new
动作时,为什么会出现此错误?
ActiveAdmin.register User do
permit_params do
permitted = [:email, :encrypted_password]
permitted << :admin if current_user.is_admin?
permitted
end
# We're overriding the new and edit controller methods to properly create users with devise. Otherwise the passwords don't get encrypted
controller do
def create
user = User.new
user.name = params[:user][:name]
user.email = params[:user][:email]
user.admin = params[:user][:admin]
user.password = params[:user][:encrypted_password]
user.password_confirmation = params[:user][:encrypted_password]
if user.save
redirect_to admin_user_path(user)
else
flash.now[:error] = user.errors.full_messages
render 'new' # THIS CAUSES THE ERROR "Couldn't find User without an ID"
#redirect_to new_admin_user_path # This redirect works just fine
end
end
end
end
日志:
Started GET "/admin/users/new" for 127.0.0.1 at 2014-03-09 21:34:35 -0500
Processing by Admin::UsersController#new as HTML
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (31.1ms)
Completed 200 OK in 37ms (Views: 34.1ms | ActiveRecord: 0.4ms)
Started GET "/admin/users/new" for 127.0.0.1 at 2014-03-09 21:34:42 -0500
Processing by Admin::UsersController#new as HTML
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (32.7ms)
Completed 200 OK in 60ms (Views: 35.4ms | ActiveRecord: 4.3ms)
Started POST "/admin/users" for 127.0.0.1 at 2014-03-09 21:34:44 -0500
Processing by Admin::UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Aa6TBt0LADDcKAHs+gFokQroSVgTnxtlgLwzvCovIcs=", "user"=>{"name"=>"", "email"=>"", "encrypted_password"=>"[FILTERED]", "admin"=>"0"}, "commit"=>"Create User"}
User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Rendered /Users/tomcaflisch/.rvm/gems/ruby-2.1.1@myapp/bundler/gems/active_admin-739b93bf9d22/app/views/active_admin/resource/new.html.arb (14.6ms)
Completed 500 Internal Server Error in 25ms
ActiveRecord::RecordNotFound - Couldn't find User without an ID:
我在这里比赛有点晚了但是碰到了这个。使用@user
而不是user
。
我遇到了这个问题,还没有找到解决方案。如果有人发现了什么,我会很高兴听到它。
由于@Catfish已经在他的代码中显示为当前的解决方法,我正在使用redirect_to
redirect_to new_admin_[resource]_path
代替
render :new
可悲的是,这样一来,用户的所有输入都会丢失
render :new
将是首选的解决方案
我遇到了一些问题,我的解决方案是这样的:
if user.save
# DO YOUR JOB
else
flash.now[:error] = user.errors.full_messages
@resource = User.new(permitted_params)
render template: 'active_admin/resource/new'
end
只需根据需要调整@resource的初始化。这个解决方案适用于我在ActiveAdmin处于提交参考'1713ec9cb43a'的状态,不知道它如何与当前/最后版本一起工作。
如果有人还在看这个,那么adysson的回答对我很有帮助。
if user.save
# DO YOUR JOB
redirect_to admin_user_path(user)
else
@resource = user
render :new
end
这样做会在输入字段下显示资源错误而不是flash消息。